blob: 374c129219ef0c48c8ff3004662c413d78ad2b4c [file] [log] [blame]
Joerg Roedelb6c02712008-06-26 21:27:53 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01003 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelb6c02712008-06-26 21:27:53 +02004 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010020#include <linux/ratelimit.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020021#include <linux/pci.h>
Joerg Roedelcb41ed82011-04-05 11:00:53 +020022#include <linux/pci-ats.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080023#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010025#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020026#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090027#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020028#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010029#include <linux/iommu.h>
Joerg Roedel815b33f2011-04-06 17:26:49 +020030#include <linux/delay.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020031#include <linux/amd-iommu.h>
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010032#include <linux/notifier.h>
33#include <linux/export.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020034#include <linux/irq.h>
35#include <linux/msi.h>
Joerg Roedel3b839a52015-04-01 14:58:47 +020036#include <linux/dma-contiguous.h>
Jiang Liu7c71d302015-04-13 14:11:33 +080037#include <linux/irqdomain.h>
Joerg Roedel5f6bed52015-12-22 13:34:22 +010038#include <linux/percpu.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020039#include <asm/irq_remapping.h>
40#include <asm/io_apic.h>
41#include <asm/apic.h>
42#include <asm/hw_irq.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020043#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020044#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090045#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010046#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020047#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020048
49#include "amd_iommu_proto.h"
50#include "amd_iommu_types.h"
Joerg Roedel6b474b82012-06-26 16:46:04 +020051#include "irq_remapping.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020052
53#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
54
Joerg Roedel815b33f2011-04-06 17:26:49 +020055#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020056
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020057/*
58 * This bitmap is used to advertise the page sizes our hardware support
59 * to the IOMMU core, which will then use this information to split
60 * physically contiguous memory regions it is mapping into page sizes
61 * that we support.
62 *
Joerg Roedel954e3dd2012-12-02 15:35:37 +010063 * 512GB Pages are not supported due to a hardware bug
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020064 */
Joerg Roedel954e3dd2012-12-02 15:35:37 +010065#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020066
Joerg Roedelb6c02712008-06-26 21:27:53 +020067static DEFINE_RWLOCK(amd_iommu_devtable_lock);
68
Joerg Roedel8fa5f802011-06-09 12:24:45 +020069/* List of all available dev_data structures */
70static LIST_HEAD(dev_data_list);
71static DEFINE_SPINLOCK(dev_data_list_lock);
72
Joerg Roedel6efed632012-06-14 15:52:58 +020073LIST_HEAD(ioapic_map);
74LIST_HEAD(hpet_map);
75
Joerg Roedel0feae532009-08-26 15:26:30 +020076/*
77 * Domain for untranslated devices - only allocated
78 * if iommu=pt passed on kernel cmd line.
79 */
Thierry Redingb22f6432014-06-27 09:03:12 +020080static const struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010081
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010082static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010083int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010084
Joerg Roedelac1534a2012-06-21 14:52:40 +020085static struct dma_map_ops amd_iommu_dma_ops;
86
Joerg Roedel431b2a22008-07-11 17:14:22 +020087/*
Joerg Roedel50917e22014-08-05 16:38:38 +020088 * This struct contains device specific data for the IOMMU
89 */
90struct iommu_dev_data {
91 struct list_head list; /* For domain->dev_list */
92 struct list_head dev_data_list; /* For global dev_data_list */
Joerg Roedel50917e22014-08-05 16:38:38 +020093 struct protection_domain *domain; /* Domain the device is bound to */
Joerg Roedel50917e22014-08-05 16:38:38 +020094 u16 devid; /* PCI Device ID */
95 bool iommu_v2; /* Device can make use of IOMMUv2 */
Joerg Roedel1e6a7b02015-07-28 16:58:48 +020096 bool passthrough; /* Device is identity mapped */
Joerg Roedel50917e22014-08-05 16:38:38 +020097 struct {
98 bool enabled;
99 int qdep;
100 } ats; /* ATS state */
101 bool pri_tlp; /* PASID TLB required for
102 PPR completions */
103 u32 errata; /* Bitmap for errata to apply */
104};
105
106/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200107 * general struct to manage commands send to an IOMMU
108 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200109struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +0200110 u32 data[4];
111};
112
Joerg Roedel05152a02012-06-15 16:53:51 +0200113struct kmem_cache *amd_iommu_irq_cache;
114
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200115static void update_domain(struct protection_domain *domain);
Joerg Roedel7a5a5662015-06-30 08:56:11 +0200116static int protection_domain_init(struct protection_domain *domain);
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100117static void detach_device(struct device *dev);
Chris Wrightc1eee672009-05-21 00:56:58 -0700118
Joerg Roedel007b74b2015-12-21 12:53:54 +0100119/*
120 * For dynamic growth the aperture size is split into ranges of 128MB of
121 * DMA address space each. This struct represents one such range.
122 */
123struct aperture_range {
124
Joerg Roedel08c5fb92015-12-21 13:04:49 +0100125 spinlock_t bitmap_lock;
126
Joerg Roedel007b74b2015-12-21 12:53:54 +0100127 /* address allocation bitmap */
128 unsigned long *bitmap;
Joerg Roedelae62d492015-12-21 16:28:45 +0100129 unsigned long offset;
Joerg Roedel60e6a7c2015-12-21 16:53:17 +0100130 unsigned long next_bit;
Joerg Roedel007b74b2015-12-21 12:53:54 +0100131
132 /*
133 * Array of PTE pages for the aperture. In this array we save all the
134 * leaf pages of the domain page table used for the aperture. This way
135 * we don't need to walk the page table to find a specific PTE. We can
136 * just calculate its address in constant time.
137 */
138 u64 *pte_pages[64];
Joerg Roedel007b74b2015-12-21 12:53:54 +0100139};
140
141/*
142 * Data container for a dma_ops specific protection domain
143 */
144struct dma_ops_domain {
145 /* generic protection domain information */
146 struct protection_domain domain;
147
148 /* size of the aperture for the mappings */
149 unsigned long aperture_size;
150
Joerg Roedelebaecb42015-12-21 18:11:32 +0100151 /* aperture index we start searching for free addresses */
Joerg Roedel5f6bed52015-12-22 13:34:22 +0100152 u32 __percpu *next_index;
Joerg Roedel007b74b2015-12-21 12:53:54 +0100153
154 /* address space relevant data */
155 struct aperture_range *aperture[APERTURE_MAX_RANGES];
Joerg Roedel007b74b2015-12-21 12:53:54 +0100156};
157
Joerg Roedel15898bb2009-11-24 15:39:42 +0100158/****************************************************************************
159 *
160 * Helper functions
161 *
162 ****************************************************************************/
163
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100164static struct protection_domain *to_pdomain(struct iommu_domain *dom)
165{
166 return container_of(dom, struct protection_domain, domain);
167}
168
Joerg Roedelf62dda62011-06-09 12:55:35 +0200169static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200170{
171 struct iommu_dev_data *dev_data;
172 unsigned long flags;
173
174 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
175 if (!dev_data)
176 return NULL;
177
Joerg Roedelf62dda62011-06-09 12:55:35 +0200178 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200179
180 spin_lock_irqsave(&dev_data_list_lock, flags);
181 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
182 spin_unlock_irqrestore(&dev_data_list_lock, flags);
183
184 return dev_data;
185}
186
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200187static struct iommu_dev_data *search_dev_data(u16 devid)
188{
189 struct iommu_dev_data *dev_data;
190 unsigned long flags;
191
192 spin_lock_irqsave(&dev_data_list_lock, flags);
193 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
194 if (dev_data->devid == devid)
195 goto out_unlock;
196 }
197
198 dev_data = NULL;
199
200out_unlock:
201 spin_unlock_irqrestore(&dev_data_list_lock, flags);
202
203 return dev_data;
204}
205
206static struct iommu_dev_data *find_dev_data(u16 devid)
207{
208 struct iommu_dev_data *dev_data;
209
210 dev_data = search_dev_data(devid);
211
212 if (dev_data == NULL)
213 dev_data = alloc_dev_data(devid);
214
215 return dev_data;
216}
217
Joerg Roedel15898bb2009-11-24 15:39:42 +0100218static inline u16 get_device_id(struct device *dev)
219{
220 struct pci_dev *pdev = to_pci_dev(dev);
221
Shuah Khan6f2729b2013-02-27 17:07:30 -0700222 return PCI_DEVID(pdev->bus->number, pdev->devfn);
Joerg Roedel15898bb2009-11-24 15:39:42 +0100223}
224
Joerg Roedel657cbb62009-11-23 15:26:46 +0100225static struct iommu_dev_data *get_dev_data(struct device *dev)
226{
227 return dev->archdata.iommu;
228}
229
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100230static bool pci_iommuv2_capable(struct pci_dev *pdev)
231{
232 static const int caps[] = {
233 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100234 PCI_EXT_CAP_ID_PRI,
235 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100236 };
237 int i, pos;
238
239 for (i = 0; i < 3; ++i) {
240 pos = pci_find_ext_capability(pdev, caps[i]);
241 if (pos == 0)
242 return false;
243 }
244
245 return true;
246}
247
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100248static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
249{
250 struct iommu_dev_data *dev_data;
251
252 dev_data = get_dev_data(&pdev->dev);
253
254 return dev_data->errata & (1 << erratum) ? true : false;
255}
256
Joerg Roedel71c70982009-11-24 16:43:06 +0100257/*
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200258 * This function actually applies the mapping to the page table of the
259 * dma_ops domain.
Joerg Roedel71c70982009-11-24 16:43:06 +0100260 */
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200261static void alloc_unity_mapping(struct dma_ops_domain *dma_dom,
262 struct unity_map_entry *e)
Joerg Roedel71c70982009-11-24 16:43:06 +0100263{
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200264 u64 addr;
Joerg Roedel71c70982009-11-24 16:43:06 +0100265
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200266 for (addr = e->address_start; addr < e->address_end;
267 addr += PAGE_SIZE) {
268 if (addr < dma_dom->aperture_size)
269 __set_bit(addr >> PAGE_SHIFT,
270 dma_dom->aperture[0]->bitmap);
Joerg Roedel71c70982009-11-24 16:43:06 +0100271 }
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200272}
Joerg Roedel71c70982009-11-24 16:43:06 +0100273
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200274/*
275 * Inits the unity mappings required for a specific device
276 */
277static void init_unity_mappings_for_device(struct device *dev,
278 struct dma_ops_domain *dma_dom)
279{
280 struct unity_map_entry *e;
281 u16 devid;
Joerg Roedel71c70982009-11-24 16:43:06 +0100282
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200283 devid = get_device_id(dev);
284
285 list_for_each_entry(e, &amd_iommu_unity_map, list) {
286 if (!(devid >= e->devid_start && devid <= e->devid_end))
287 continue;
288 alloc_unity_mapping(dma_dom, e);
289 }
Joerg Roedel71c70982009-11-24 16:43:06 +0100290}
291
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100292/*
293 * This function checks if the driver got a valid device from the caller to
294 * avoid dereferencing invalid pointers.
295 */
296static bool check_device(struct device *dev)
297{
298 u16 devid;
299
300 if (!dev || !dev->dma_mask)
301 return false;
302
Yijing Wangb82a2272013-12-05 19:42:41 +0800303 /* No PCI device */
304 if (!dev_is_pci(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100305 return false;
306
307 devid = get_device_id(dev);
308
309 /* Out of our scope? */
310 if (devid > amd_iommu_last_bdf)
311 return false;
312
313 if (amd_iommu_rlookup_table[devid] == NULL)
314 return false;
315
316 return true;
317}
318
Alex Williamson25b11ce2014-09-19 10:03:13 -0600319static void init_iommu_group(struct device *dev)
Alex Williamson2851db22012-10-08 22:49:41 -0600320{
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200321 struct dma_ops_domain *dma_domain;
322 struct iommu_domain *domain;
Alex Williamson2851db22012-10-08 22:49:41 -0600323 struct iommu_group *group;
Alex Williamson2851db22012-10-08 22:49:41 -0600324
Alex Williamson65d53522014-07-03 09:51:30 -0600325 group = iommu_group_get_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200326 if (IS_ERR(group))
327 return;
328
329 domain = iommu_group_default_domain(group);
330 if (!domain)
331 goto out;
332
333 dma_domain = to_pdomain(domain)->priv;
334
335 init_unity_mappings_for_device(dev, dma_domain);
336out:
337 iommu_group_put(group);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600338}
339
340static int iommu_init_device(struct device *dev)
341{
342 struct pci_dev *pdev = to_pci_dev(dev);
343 struct iommu_dev_data *dev_data;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600344
345 if (dev->archdata.iommu)
346 return 0;
347
348 dev_data = find_dev_data(get_device_id(dev));
349 if (!dev_data)
350 return -ENOMEM;
351
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100352 if (pci_iommuv2_capable(pdev)) {
353 struct amd_iommu *iommu;
354
355 iommu = amd_iommu_rlookup_table[dev_data->devid];
356 dev_data->iommu_v2 = iommu->is_iommu_v2;
357 }
358
Joerg Roedel657cbb62009-11-23 15:26:46 +0100359 dev->archdata.iommu = dev_data;
360
Alex Williamson066f2e92014-06-12 16:12:37 -0600361 iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
362 dev);
363
Joerg Roedel657cbb62009-11-23 15:26:46 +0100364 return 0;
365}
366
Joerg Roedel26018872011-06-06 16:50:14 +0200367static void iommu_ignore_device(struct device *dev)
368{
369 u16 devid, alias;
370
371 devid = get_device_id(dev);
372 alias = amd_iommu_alias_table[devid];
373
374 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
375 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
376
377 amd_iommu_rlookup_table[devid] = NULL;
378 amd_iommu_rlookup_table[alias] = NULL;
379}
380
Joerg Roedel657cbb62009-11-23 15:26:46 +0100381static void iommu_uninit_device(struct device *dev)
382{
Alex Williamsonc1931092014-07-03 09:51:24 -0600383 struct iommu_dev_data *dev_data = search_dev_data(get_device_id(dev));
384
385 if (!dev_data)
386 return;
387
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100388 if (dev_data->domain)
389 detach_device(dev);
390
Alex Williamson066f2e92014-06-12 16:12:37 -0600391 iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
392 dev);
393
Alex Williamson9dcd6132012-05-30 14:19:07 -0600394 iommu_group_remove_device(dev);
395
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200396 /* Remove dma-ops */
397 dev->archdata.dma_ops = NULL;
398
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200399 /*
Alex Williamsonc1931092014-07-03 09:51:24 -0600400 * We keep dev_data around for unplugged devices and reuse it when the
401 * device is re-plugged - not doing so would introduce a ton of races.
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200402 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100403}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100404
Joerg Roedel7f265082008-12-12 13:50:21 +0100405#ifdef CONFIG_AMD_IOMMU_STATS
406
407/*
408 * Initialization code for statistics collection
409 */
410
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100411DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100412DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100413DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100414DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100415DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100416DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100417DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100418DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100419DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100420DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100421DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100422DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100423DECLARE_STATS_COUNTER(complete_ppr);
424DECLARE_STATS_COUNTER(invalidate_iotlb);
425DECLARE_STATS_COUNTER(invalidate_iotlb_all);
426DECLARE_STATS_COUNTER(pri_requests);
427
Joerg Roedel7f265082008-12-12 13:50:21 +0100428static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100429static struct dentry *de_fflush;
430
431static void amd_iommu_stats_add(struct __iommu_counter *cnt)
432{
433 if (stats_dir == NULL)
434 return;
435
436 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
437 &cnt->value);
438}
439
440static void amd_iommu_stats_init(void)
441{
442 stats_dir = debugfs_create_dir("amd-iommu", NULL);
443 if (stats_dir == NULL)
444 return;
445
Joerg Roedel7f265082008-12-12 13:50:21 +0100446 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300447 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100448
449 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100450 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100451 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100452 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100453 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100454 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100455 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100456 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100457 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100458 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100459 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100460 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100461 amd_iommu_stats_add(&complete_ppr);
462 amd_iommu_stats_add(&invalidate_iotlb);
463 amd_iommu_stats_add(&invalidate_iotlb_all);
464 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100465}
466
467#endif
468
Joerg Roedel431b2a22008-07-11 17:14:22 +0200469/****************************************************************************
470 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200471 * Interrupt handling functions
472 *
473 ****************************************************************************/
474
Joerg Roedele3e59872009-09-03 14:02:10 +0200475static void dump_dte_entry(u16 devid)
476{
477 int i;
478
Joerg Roedelee6c2862011-11-09 12:06:03 +0100479 for (i = 0; i < 4; ++i)
480 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200481 amd_iommu_dev_table[devid].data[i]);
482}
483
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200484static void dump_command(unsigned long phys_addr)
485{
486 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
487 int i;
488
489 for (i = 0; i < 4; ++i)
490 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
491}
492
Joerg Roedela345b232009-09-03 15:01:43 +0200493static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200494{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200495 int type, devid, domid, flags;
496 volatile u32 *event = __evt;
497 int count = 0;
498 u64 address;
499
500retry:
501 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
502 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
503 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
504 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
505 address = (u64)(((u64)event[3]) << 32) | event[2];
506
507 if (type == 0) {
508 /* Did we hit the erratum? */
509 if (++count == LOOP_TIMEOUT) {
510 pr_err("AMD-Vi: No event written to event log\n");
511 return;
512 }
513 udelay(1);
514 goto retry;
515 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200516
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200517 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200518
519 switch (type) {
520 case EVENT_TYPE_ILL_DEV:
521 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
522 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700523 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200524 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200525 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200526 break;
527 case EVENT_TYPE_IO_FAULT:
528 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
529 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700530 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200531 domid, address, flags);
532 break;
533 case EVENT_TYPE_DEV_TAB_ERR:
534 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
535 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700536 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200537 address, flags);
538 break;
539 case EVENT_TYPE_PAGE_TAB_ERR:
540 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
541 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700542 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200543 domid, address, flags);
544 break;
545 case EVENT_TYPE_ILL_CMD:
546 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200547 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200548 break;
549 case EVENT_TYPE_CMD_HARD_ERR:
550 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
551 "flags=0x%04x]\n", address, flags);
552 break;
553 case EVENT_TYPE_IOTLB_INV_TO:
554 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
555 "address=0x%016llx]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700556 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200557 address);
558 break;
559 case EVENT_TYPE_INV_DEV_REQ:
560 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
561 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700562 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200563 address, flags);
564 break;
565 default:
566 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
567 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200568
569 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200570}
571
572static void iommu_poll_events(struct amd_iommu *iommu)
573{
574 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200575
576 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
577 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
578
579 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200580 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200581 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200582 }
583
584 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200585}
586
Joerg Roedeleee53532012-06-01 15:20:23 +0200587static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100588{
589 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100590
Joerg Roedel399be2f2011-12-01 16:53:47 +0100591 INC_STATS_COUNTER(pri_requests);
592
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100593 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
594 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
595 return;
596 }
597
598 fault.address = raw[1];
599 fault.pasid = PPR_PASID(raw[0]);
600 fault.device_id = PPR_DEVID(raw[0]);
601 fault.tag = PPR_TAG(raw[0]);
602 fault.flags = PPR_FLAGS(raw[0]);
603
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100604 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
605}
606
607static void iommu_poll_ppr_log(struct amd_iommu *iommu)
608{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100609 u32 head, tail;
610
611 if (iommu->ppr_log == NULL)
612 return;
613
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100614 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
615 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
616
617 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200618 volatile u64 *raw;
619 u64 entry[2];
620 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100621
Joerg Roedeleee53532012-06-01 15:20:23 +0200622 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100623
Joerg Roedeleee53532012-06-01 15:20:23 +0200624 /*
625 * Hardware bug: Interrupt may arrive before the entry is
626 * written to memory. If this happens we need to wait for the
627 * entry to arrive.
628 */
629 for (i = 0; i < LOOP_TIMEOUT; ++i) {
630 if (PPR_REQ_TYPE(raw[0]) != 0)
631 break;
632 udelay(1);
633 }
634
635 /* Avoid memcpy function-call overhead */
636 entry[0] = raw[0];
637 entry[1] = raw[1];
638
639 /*
640 * To detect the hardware bug we need to clear the entry
641 * back to zero.
642 */
643 raw[0] = raw[1] = 0UL;
644
645 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100646 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
647 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200648
Joerg Roedeleee53532012-06-01 15:20:23 +0200649 /* Handle PPR entry */
650 iommu_handle_ppr_entry(iommu, entry);
651
Joerg Roedeleee53532012-06-01 15:20:23 +0200652 /* Refresh ring-buffer information */
653 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100654 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
655 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100656}
657
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200658irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200659{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500660 struct amd_iommu *iommu = (struct amd_iommu *) data;
661 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200662
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500663 while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
664 /* Enable EVT and PPR interrupts again */
665 writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
666 iommu->mmio_base + MMIO_STATUS_OFFSET);
667
668 if (status & MMIO_STATUS_EVT_INT_MASK) {
669 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
670 iommu_poll_events(iommu);
671 }
672
673 if (status & MMIO_STATUS_PPR_INT_MASK) {
674 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
675 iommu_poll_ppr_log(iommu);
676 }
677
678 /*
679 * Hardware bug: ERBT1312
680 * When re-enabling interrupt (by writing 1
681 * to clear the bit), the hardware might also try to set
682 * the interrupt bit in the event status register.
683 * In this scenario, the bit will be set, and disable
684 * subsequent interrupts.
685 *
686 * Workaround: The IOMMU driver should read back the
687 * status register and check if the interrupt bits are cleared.
688 * If not, driver will need to go through the interrupt handler
689 * again and re-clear the bits
690 */
691 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100692 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200693 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200694}
695
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200696irqreturn_t amd_iommu_int_handler(int irq, void *data)
697{
698 return IRQ_WAKE_THREAD;
699}
700
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200701/****************************************************************************
702 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200703 * IOMMU command queuing functions
704 *
705 ****************************************************************************/
706
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200707static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200708{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200709 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200710
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200711 while (*sem == 0 && i < LOOP_TIMEOUT) {
712 udelay(1);
713 i += 1;
714 }
715
716 if (i == LOOP_TIMEOUT) {
717 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
718 return -EIO;
719 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200720
721 return 0;
722}
723
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200724static void copy_cmd_to_buffer(struct amd_iommu *iommu,
725 struct iommu_cmd *cmd,
726 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200727{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200728 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200729
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200730 target = iommu->cmd_buf + tail;
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200731 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200732
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200733 /* Copy command to buffer */
734 memcpy(target, cmd, sizeof(*cmd));
735
736 /* Tell the IOMMU about it */
737 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
738}
739
Joerg Roedel815b33f2011-04-06 17:26:49 +0200740static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200741{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200742 WARN_ON(address & 0x7ULL);
743
Joerg Roedelded46732011-04-06 10:53:48 +0200744 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200745 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
746 cmd->data[1] = upper_32_bits(__pa(address));
747 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200748 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
749}
750
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200751static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
752{
753 memset(cmd, 0, sizeof(*cmd));
754 cmd->data[0] = devid;
755 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
756}
757
Joerg Roedel11b64022011-04-06 11:49:28 +0200758static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
759 size_t size, u16 domid, int pde)
760{
761 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100762 bool s;
Joerg Roedel11b64022011-04-06 11:49:28 +0200763
764 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100765 s = false;
Joerg Roedel11b64022011-04-06 11:49:28 +0200766
767 if (pages > 1) {
768 /*
769 * If we have to flush more than one page, flush all
770 * TLB entries for this domain
771 */
772 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100773 s = true;
Joerg Roedel11b64022011-04-06 11:49:28 +0200774 }
775
776 address &= PAGE_MASK;
777
778 memset(cmd, 0, sizeof(*cmd));
779 cmd->data[1] |= domid;
780 cmd->data[2] = lower_32_bits(address);
781 cmd->data[3] = upper_32_bits(address);
782 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
783 if (s) /* size bit - we flush more than one 4kb page */
784 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200785 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200786 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
787}
788
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200789static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
790 u64 address, size_t size)
791{
792 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100793 bool s;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200794
795 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100796 s = false;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200797
798 if (pages > 1) {
799 /*
800 * If we have to flush more than one page, flush all
801 * TLB entries for this domain
802 */
803 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100804 s = true;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200805 }
806
807 address &= PAGE_MASK;
808
809 memset(cmd, 0, sizeof(*cmd));
810 cmd->data[0] = devid;
811 cmd->data[0] |= (qdep & 0xff) << 24;
812 cmd->data[1] = devid;
813 cmd->data[2] = lower_32_bits(address);
814 cmd->data[3] = upper_32_bits(address);
815 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
816 if (s)
817 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
818}
819
Joerg Roedel22e266c2011-11-21 15:59:08 +0100820static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
821 u64 address, bool size)
822{
823 memset(cmd, 0, sizeof(*cmd));
824
825 address &= ~(0xfffULL);
826
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600827 cmd->data[0] = pasid;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100828 cmd->data[1] = domid;
829 cmd->data[2] = lower_32_bits(address);
830 cmd->data[3] = upper_32_bits(address);
831 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
832 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
833 if (size)
834 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
835 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
836}
837
838static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
839 int qdep, u64 address, bool size)
840{
841 memset(cmd, 0, sizeof(*cmd));
842
843 address &= ~(0xfffULL);
844
845 cmd->data[0] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600846 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100847 cmd->data[0] |= (qdep & 0xff) << 24;
848 cmd->data[1] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600849 cmd->data[1] |= (pasid & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100850 cmd->data[2] = lower_32_bits(address);
851 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
852 cmd->data[3] = upper_32_bits(address);
853 if (size)
854 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
855 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
856}
857
Joerg Roedelc99afa22011-11-21 18:19:25 +0100858static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
859 int status, int tag, bool gn)
860{
861 memset(cmd, 0, sizeof(*cmd));
862
863 cmd->data[0] = devid;
864 if (gn) {
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600865 cmd->data[1] = pasid;
Joerg Roedelc99afa22011-11-21 18:19:25 +0100866 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
867 }
868 cmd->data[3] = tag & 0x1ff;
869 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
870
871 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
872}
873
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200874static void build_inv_all(struct iommu_cmd *cmd)
875{
876 memset(cmd, 0, sizeof(*cmd));
877 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200878}
879
Joerg Roedel7ef27982012-06-21 16:46:04 +0200880static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
881{
882 memset(cmd, 0, sizeof(*cmd));
883 cmd->data[0] = devid;
884 CMD_SET_TYPE(cmd, CMD_INV_IRT);
885}
886
Joerg Roedel431b2a22008-07-11 17:14:22 +0200887/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200888 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200889 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200890 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200891static int iommu_queue_command_sync(struct amd_iommu *iommu,
892 struct iommu_cmd *cmd,
893 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200894{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200895 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200896 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200897
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200898again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200899 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200900
901 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
902 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200903 next_tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
904 left = (head - next_tail) % CMD_BUFFER_SIZE;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200905
906 if (left <= 2) {
907 struct iommu_cmd sync_cmd;
908 volatile u64 sem = 0;
909 int ret;
910
911 build_completion_wait(&sync_cmd, (u64)&sem);
912 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
913
914 spin_unlock_irqrestore(&iommu->lock, flags);
915
916 if ((ret = wait_on_sem(&sem)) != 0)
917 return ret;
918
919 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200920 }
921
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200922 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200923
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200924 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200925 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200926
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200927 spin_unlock_irqrestore(&iommu->lock, flags);
928
Joerg Roedel815b33f2011-04-06 17:26:49 +0200929 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100930}
931
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200932static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
933{
934 return iommu_queue_command_sync(iommu, cmd, true);
935}
936
Joerg Roedel8d201962008-12-02 20:34:41 +0100937/*
938 * This function queues a completion wait command into the command
939 * buffer of an IOMMU
940 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100941static int iommu_completion_wait(struct amd_iommu *iommu)
942{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200943 struct iommu_cmd cmd;
944 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200945 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100946
947 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200948 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100949
Joerg Roedel815b33f2011-04-06 17:26:49 +0200950 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100951
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200952 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100953 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200954 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100955
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200956 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200957}
958
Joerg Roedeld8c13082011-04-06 18:51:26 +0200959static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200960{
961 struct iommu_cmd cmd;
962
Joerg Roedeld8c13082011-04-06 18:51:26 +0200963 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200964
Joerg Roedeld8c13082011-04-06 18:51:26 +0200965 return iommu_queue_command(iommu, &cmd);
966}
967
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200968static void iommu_flush_dte_all(struct amd_iommu *iommu)
969{
970 u32 devid;
971
972 for (devid = 0; devid <= 0xffff; ++devid)
973 iommu_flush_dte(iommu, devid);
974
975 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200976}
977
978/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200979 * This function uses heavy locking and may disable irqs for some time. But
980 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200981 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200982static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200983{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200984 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200985
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200986 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
987 struct iommu_cmd cmd;
988 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
989 dom_id, 1);
990 iommu_queue_command(iommu, &cmd);
991 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200992
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200993 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200994}
995
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200996static void iommu_flush_all(struct amd_iommu *iommu)
997{
998 struct iommu_cmd cmd;
999
1000 build_inv_all(&cmd);
1001
1002 iommu_queue_command(iommu, &cmd);
1003 iommu_completion_wait(iommu);
1004}
1005
Joerg Roedel7ef27982012-06-21 16:46:04 +02001006static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1007{
1008 struct iommu_cmd cmd;
1009
1010 build_inv_irt(&cmd, devid);
1011
1012 iommu_queue_command(iommu, &cmd);
1013}
1014
1015static void iommu_flush_irt_all(struct amd_iommu *iommu)
1016{
1017 u32 devid;
1018
1019 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1020 iommu_flush_irt(iommu, devid);
1021
1022 iommu_completion_wait(iommu);
1023}
1024
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001025void iommu_flush_all_caches(struct amd_iommu *iommu)
1026{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001027 if (iommu_feature(iommu, FEATURE_IA)) {
1028 iommu_flush_all(iommu);
1029 } else {
1030 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001031 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001032 iommu_flush_tlb_all(iommu);
1033 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001034}
1035
Joerg Roedel431b2a22008-07-11 17:14:22 +02001036/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001037 * Command send function for flushing on-device TLB
1038 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001039static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1040 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001041{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001042 struct amd_iommu *iommu;
1043 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001044 int qdep;
1045
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001046 qdep = dev_data->ats.qdep;
1047 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001048
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001049 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001050
1051 return iommu_queue_command(iommu, &cmd);
1052}
1053
1054/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001055 * Command send function for invalidating a device table entry
1056 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001057static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001058{
1059 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001060 u16 alias;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001061 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001062
Joerg Roedel6c542042011-06-09 17:07:31 +02001063 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele25bfb52015-10-20 17:33:38 +02001064 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001065
Joerg Roedelf62dda62011-06-09 12:55:35 +02001066 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001067 if (!ret && alias != dev_data->devid)
1068 ret = iommu_flush_dte(iommu, alias);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001069 if (ret)
1070 return ret;
1071
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001072 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001073 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001074
1075 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001076}
1077
Joerg Roedel431b2a22008-07-11 17:14:22 +02001078/*
1079 * TLB invalidation function which is called from the mapping functions.
1080 * It invalidates a single PTE if the range to flush is within a single
1081 * page. Otherwise it flushes the whole TLB of the IOMMU.
1082 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001083static void __domain_flush_pages(struct protection_domain *domain,
1084 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001085{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001086 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001087 struct iommu_cmd cmd;
1088 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001089
Joerg Roedel11b64022011-04-06 11:49:28 +02001090 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001091
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001092 for (i = 0; i < amd_iommus_present; ++i) {
1093 if (!domain->dev_iommu[i])
1094 continue;
1095
1096 /*
1097 * Devices of this domain are behind this IOMMU
1098 * We need a TLB flush
1099 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001100 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001101 }
1102
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001103 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001104
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001105 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001106 continue;
1107
Joerg Roedel6c542042011-06-09 17:07:31 +02001108 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001109 }
1110
Joerg Roedel11b64022011-04-06 11:49:28 +02001111 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001112}
1113
Joerg Roedel17b124b2011-04-06 18:01:35 +02001114static void domain_flush_pages(struct protection_domain *domain,
1115 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001116{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001117 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001118}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001119
Joerg Roedel1c655772008-09-04 18:40:05 +02001120/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001121static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001122{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001123 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001124}
1125
Chris Wright42a49f92009-06-15 15:42:00 +02001126/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001127static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001128{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001129 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1130}
1131
1132static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001133{
1134 int i;
1135
1136 for (i = 0; i < amd_iommus_present; ++i) {
1137 if (!domain->dev_iommu[i])
1138 continue;
1139
1140 /*
1141 * Devices of this domain are behind this IOMMU
1142 * We need to wait for completion of all commands.
1143 */
1144 iommu_completion_wait(amd_iommus[i]);
1145 }
1146}
1147
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001148
Joerg Roedel43f49602008-12-02 21:01:12 +01001149/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001150 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001151 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001152static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001153{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001154 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001155
1156 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001157 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001158}
1159
Joerg Roedel431b2a22008-07-11 17:14:22 +02001160/****************************************************************************
1161 *
1162 * The functions below are used the create the page table mappings for
1163 * unity mapped regions.
1164 *
1165 ****************************************************************************/
1166
1167/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001168 * This function is used to add another level to an IO page table. Adding
1169 * another level increases the size of the address space by 9 bits to a size up
1170 * to 64 bits.
1171 */
1172static bool increase_address_space(struct protection_domain *domain,
1173 gfp_t gfp)
1174{
1175 u64 *pte;
1176
1177 if (domain->mode == PAGE_MODE_6_LEVEL)
1178 /* address space already 64 bit large */
1179 return false;
1180
1181 pte = (void *)get_zeroed_page(gfp);
1182 if (!pte)
1183 return false;
1184
1185 *pte = PM_LEVEL_PDE(domain->mode,
1186 virt_to_phys(domain->pt_root));
1187 domain->pt_root = pte;
1188 domain->mode += 1;
1189 domain->updated = true;
1190
1191 return true;
1192}
1193
1194static u64 *alloc_pte(struct protection_domain *domain,
1195 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001196 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001197 u64 **pte_page,
1198 gfp_t gfp)
1199{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001200 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001201 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001202
1203 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001204
1205 while (address > PM_LEVEL_SIZE(domain->mode))
1206 increase_address_space(domain, gfp);
1207
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001208 level = domain->mode - 1;
1209 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1210 address = PAGE_SIZE_ALIGN(address, page_size);
1211 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001212
1213 while (level > end_lvl) {
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001214 u64 __pte, __npte;
1215
1216 __pte = *pte;
1217
1218 if (!IOMMU_PTE_PRESENT(__pte)) {
Joerg Roedel308973d2009-11-24 17:43:32 +01001219 page = (u64 *)get_zeroed_page(gfp);
1220 if (!page)
1221 return NULL;
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001222
1223 __npte = PM_LEVEL_PDE(level, virt_to_phys(page));
1224
1225 if (cmpxchg64(pte, __pte, __npte)) {
1226 free_page((unsigned long)page);
1227 continue;
1228 }
Joerg Roedel308973d2009-11-24 17:43:32 +01001229 }
1230
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001231 /* No level skipping support yet */
1232 if (PM_PTE_LEVEL(*pte) != level)
1233 return NULL;
1234
Joerg Roedel308973d2009-11-24 17:43:32 +01001235 level -= 1;
1236
1237 pte = IOMMU_PTE_PAGE(*pte);
1238
1239 if (pte_page && level == end_lvl)
1240 *pte_page = pte;
1241
1242 pte = &pte[PM_LEVEL_INDEX(level, address)];
1243 }
1244
1245 return pte;
1246}
1247
1248/*
1249 * This function checks if there is a PTE for a given dma address. If
1250 * there is one, it returns the pointer to it.
1251 */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001252static u64 *fetch_pte(struct protection_domain *domain,
1253 unsigned long address,
1254 unsigned long *page_size)
Joerg Roedel308973d2009-11-24 17:43:32 +01001255{
1256 int level;
1257 u64 *pte;
1258
Joerg Roedel24cd7722010-01-19 17:27:39 +01001259 if (address > PM_LEVEL_SIZE(domain->mode))
1260 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001261
Joerg Roedel3039ca12015-04-01 14:58:48 +02001262 level = domain->mode - 1;
1263 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1264 *page_size = PTE_LEVEL_PAGE_SIZE(level);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001265
1266 while (level > 0) {
1267
1268 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001269 if (!IOMMU_PTE_PRESENT(*pte))
1270 return NULL;
1271
Joerg Roedel24cd7722010-01-19 17:27:39 +01001272 /* Large PTE */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001273 if (PM_PTE_LEVEL(*pte) == 7 ||
1274 PM_PTE_LEVEL(*pte) == 0)
1275 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001276
1277 /* No level skipping support yet */
1278 if (PM_PTE_LEVEL(*pte) != level)
1279 return NULL;
1280
Joerg Roedel308973d2009-11-24 17:43:32 +01001281 level -= 1;
1282
Joerg Roedel24cd7722010-01-19 17:27:39 +01001283 /* Walk to the next level */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001284 pte = IOMMU_PTE_PAGE(*pte);
1285 pte = &pte[PM_LEVEL_INDEX(level, address)];
1286 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1287 }
1288
1289 if (PM_PTE_LEVEL(*pte) == 0x07) {
1290 unsigned long pte_mask;
1291
1292 /*
1293 * If we have a series of large PTEs, make
1294 * sure to return a pointer to the first one.
1295 */
1296 *page_size = pte_mask = PTE_PAGE_SIZE(*pte);
1297 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1298 pte = (u64 *)(((unsigned long)pte) & pte_mask);
Joerg Roedel308973d2009-11-24 17:43:32 +01001299 }
1300
1301 return pte;
1302}
1303
1304/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001305 * Generic mapping functions. It maps a physical address into a DMA
1306 * address space. It allocates the page table pages if necessary.
1307 * In the future it can be extended to a generic mapping function
1308 * supporting all features of AMD IOMMU page tables like level skipping
1309 * and full 64 bit address spaces.
1310 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001311static int iommu_map_page(struct protection_domain *dom,
1312 unsigned long bus_addr,
1313 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001314 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001315 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001316{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001317 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001318 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001319
Joerg Roedeld4b03662015-04-01 14:58:52 +02001320 BUG_ON(!IS_ALIGNED(bus_addr, page_size));
1321 BUG_ON(!IS_ALIGNED(phys_addr, page_size));
1322
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001323 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001324 return -EINVAL;
1325
Joerg Roedeld4b03662015-04-01 14:58:52 +02001326 count = PAGE_SIZE_PTE_COUNT(page_size);
1327 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001328
Maurizio Lombardi63eaa752014-09-11 12:28:03 +02001329 if (!pte)
1330 return -ENOMEM;
1331
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001332 for (i = 0; i < count; ++i)
1333 if (IOMMU_PTE_PRESENT(pte[i]))
1334 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001335
Joerg Roedeld4b03662015-04-01 14:58:52 +02001336 if (count > 1) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001337 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1338 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1339 } else
1340 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1341
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001342 if (prot & IOMMU_PROT_IR)
1343 __pte |= IOMMU_PTE_IR;
1344 if (prot & IOMMU_PROT_IW)
1345 __pte |= IOMMU_PTE_IW;
1346
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001347 for (i = 0; i < count; ++i)
1348 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001349
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001350 update_domain(dom);
1351
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001352 return 0;
1353}
1354
Joerg Roedel24cd7722010-01-19 17:27:39 +01001355static unsigned long iommu_unmap_page(struct protection_domain *dom,
1356 unsigned long bus_addr,
1357 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001358{
Joerg Roedel71b390e2015-04-01 14:58:49 +02001359 unsigned long long unmapped;
1360 unsigned long unmap_size;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001361 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001362
Joerg Roedel24cd7722010-01-19 17:27:39 +01001363 BUG_ON(!is_power_of_2(page_size));
1364
1365 unmapped = 0;
1366
1367 while (unmapped < page_size) {
1368
Joerg Roedel71b390e2015-04-01 14:58:49 +02001369 pte = fetch_pte(dom, bus_addr, &unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001370
Joerg Roedel71b390e2015-04-01 14:58:49 +02001371 if (pte) {
1372 int i, count;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001373
Joerg Roedel71b390e2015-04-01 14:58:49 +02001374 count = PAGE_SIZE_PTE_COUNT(unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001375 for (i = 0; i < count; i++)
1376 pte[i] = 0ULL;
1377 }
1378
1379 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1380 unmapped += unmap_size;
1381 }
1382
Alex Williamson60d0ca32013-06-21 14:33:19 -06001383 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001384
1385 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001386}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001387
Joerg Roedel431b2a22008-07-11 17:14:22 +02001388/****************************************************************************
1389 *
1390 * The next functions belong to the address allocator for the dma_ops
1391 * interface functions. They work like the allocators in the other IOMMU
1392 * drivers. Its basically a bitmap which marks the allocated pages in
1393 * the aperture. Maybe it could be enhanced in the future to a more
1394 * efficient allocator.
1395 *
1396 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001397
Joerg Roedel431b2a22008-07-11 17:14:22 +02001398/*
Joerg Roedel384de722009-05-15 12:30:05 +02001399 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001400 *
1401 * called with domain->lock held
1402 */
Joerg Roedel384de722009-05-15 12:30:05 +02001403
Joerg Roedel9cabe892009-05-18 16:38:55 +02001404/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001405 * Used to reserve address ranges in the aperture (e.g. for exclusion
1406 * ranges.
1407 */
1408static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1409 unsigned long start_page,
1410 unsigned int pages)
1411{
1412 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1413
1414 if (start_page + pages > last_page)
1415 pages = last_page - start_page;
1416
1417 for (i = start_page; i < start_page + pages; ++i) {
1418 int index = i / APERTURE_RANGE_PAGES;
1419 int page = i % APERTURE_RANGE_PAGES;
1420 __set_bit(page, dom->aperture[index]->bitmap);
1421 }
1422}
1423
1424/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001425 * This function is used to add a new aperture range to an existing
1426 * aperture in case of dma_ops domain allocation or address allocation
1427 * failure.
1428 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001429static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001430 bool populate, gfp_t gfp)
1431{
1432 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001433 unsigned long i, old_size, pte_pgsize;
Joerg Roedela73c1562015-12-21 19:25:56 +01001434 struct aperture_range *range;
1435 struct amd_iommu *iommu;
1436 unsigned long flags;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001437
Joerg Roedelf5e97052009-05-22 12:31:53 +02001438#ifdef CONFIG_IOMMU_STRESS
1439 populate = false;
1440#endif
1441
Joerg Roedel9cabe892009-05-18 16:38:55 +02001442 if (index >= APERTURE_MAX_RANGES)
1443 return -ENOMEM;
1444
Joerg Roedela73c1562015-12-21 19:25:56 +01001445 range = kzalloc(sizeof(struct aperture_range), gfp);
1446 if (!range)
Joerg Roedel9cabe892009-05-18 16:38:55 +02001447 return -ENOMEM;
1448
Joerg Roedela73c1562015-12-21 19:25:56 +01001449 range->bitmap = (void *)get_zeroed_page(gfp);
1450 if (!range->bitmap)
Joerg Roedel9cabe892009-05-18 16:38:55 +02001451 goto out_free;
1452
Joerg Roedela73c1562015-12-21 19:25:56 +01001453 range->offset = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001454
Joerg Roedela73c1562015-12-21 19:25:56 +01001455 spin_lock_init(&range->bitmap_lock);
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001456
Joerg Roedel9cabe892009-05-18 16:38:55 +02001457 if (populate) {
1458 unsigned long address = dma_dom->aperture_size;
1459 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1460 u64 *pte, *pte_page;
1461
1462 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001463 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001464 &pte_page, gfp);
1465 if (!pte)
1466 goto out_free;
1467
Joerg Roedela73c1562015-12-21 19:25:56 +01001468 range->pte_pages[i] = pte_page;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001469
1470 address += APERTURE_RANGE_SIZE / 64;
1471 }
1472 }
1473
Joerg Roedel92d420e2015-12-21 19:31:33 +01001474 spin_lock_irqsave(&dma_dom->domain.lock, flags);
1475
Joerg Roedela73c1562015-12-21 19:25:56 +01001476 /* First take the bitmap_lock and then publish the range */
Joerg Roedel92d420e2015-12-21 19:31:33 +01001477 spin_lock(&range->bitmap_lock);
Joerg Roedela73c1562015-12-21 19:25:56 +01001478
1479 old_size = dma_dom->aperture_size;
1480 dma_dom->aperture[index] = range;
1481 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001482
Joerg Roedel17f5b562011-07-06 17:14:44 +02001483 /* Reserve address range used for MSI messages */
1484 if (old_size < MSI_ADDR_BASE_LO &&
1485 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1486 unsigned long spage;
1487 int pages;
1488
1489 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1490 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1491
1492 dma_ops_reserve_addresses(dma_dom, spage, pages);
1493 }
1494
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001495 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001496 for_each_iommu(iommu) {
1497 if (iommu->exclusion_start &&
1498 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1499 && iommu->exclusion_start < dma_dom->aperture_size) {
1500 unsigned long startpage;
1501 int pages = iommu_num_pages(iommu->exclusion_start,
1502 iommu->exclusion_length,
1503 PAGE_SIZE);
1504 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1505 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1506 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001507 }
1508
1509 /*
1510 * Check for areas already mapped as present in the new aperture
1511 * range and mark those pages as reserved in the allocator. Such
1512 * mappings may already exist as a result of requested unity
1513 * mappings for devices.
1514 */
1515 for (i = dma_dom->aperture[index]->offset;
1516 i < dma_dom->aperture_size;
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001517 i += pte_pgsize) {
Joerg Roedel3039ca12015-04-01 14:58:48 +02001518 u64 *pte = fetch_pte(&dma_dom->domain, i, &pte_pgsize);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001519 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1520 continue;
1521
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001522 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT,
1523 pte_pgsize >> 12);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001524 }
1525
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001526 update_domain(&dma_dom->domain);
1527
Joerg Roedel92d420e2015-12-21 19:31:33 +01001528 spin_unlock(&range->bitmap_lock);
1529
1530 spin_unlock_irqrestore(&dma_dom->domain.lock, flags);
Joerg Roedela73c1562015-12-21 19:25:56 +01001531
Joerg Roedel9cabe892009-05-18 16:38:55 +02001532 return 0;
1533
1534out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001535 update_domain(&dma_dom->domain);
1536
Joerg Roedela73c1562015-12-21 19:25:56 +01001537 free_page((unsigned long)range->bitmap);
Joerg Roedel9cabe892009-05-18 16:38:55 +02001538
Joerg Roedela73c1562015-12-21 19:25:56 +01001539 kfree(range);
Joerg Roedel9cabe892009-05-18 16:38:55 +02001540
1541 return -ENOMEM;
1542}
1543
Joerg Roedelccb50e02015-12-21 17:49:34 +01001544static dma_addr_t dma_ops_aperture_alloc(struct dma_ops_domain *dom,
1545 struct aperture_range *range,
Joerg Roedela0f51442015-12-21 16:20:09 +01001546 unsigned long pages,
Joerg Roedela0f51442015-12-21 16:20:09 +01001547 unsigned long dma_mask,
1548 unsigned long boundary_size,
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001549 unsigned long align_mask,
1550 bool trylock)
Joerg Roedela0f51442015-12-21 16:20:09 +01001551{
1552 unsigned long offset, limit, flags;
1553 dma_addr_t address;
Joerg Roedelccb50e02015-12-21 17:49:34 +01001554 bool flush = false;
Joerg Roedela0f51442015-12-21 16:20:09 +01001555
1556 offset = range->offset >> PAGE_SHIFT;
1557 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1558 dma_mask >> PAGE_SHIFT);
1559
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001560 if (trylock) {
1561 if (!spin_trylock_irqsave(&range->bitmap_lock, flags))
1562 return -1;
1563 } else {
1564 spin_lock_irqsave(&range->bitmap_lock, flags);
1565 }
1566
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001567 address = iommu_area_alloc(range->bitmap, limit, range->next_bit,
1568 pages, offset, boundary_size, align_mask);
Joerg Roedelccb50e02015-12-21 17:49:34 +01001569 if (address == -1) {
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001570 /* Nothing found, retry one time */
1571 address = iommu_area_alloc(range->bitmap, limit,
1572 0, pages, offset, boundary_size,
1573 align_mask);
Joerg Roedelccb50e02015-12-21 17:49:34 +01001574 flush = true;
1575 }
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001576
1577 if (address != -1)
1578 range->next_bit = address + pages;
1579
Joerg Roedela0f51442015-12-21 16:20:09 +01001580 spin_unlock_irqrestore(&range->bitmap_lock, flags);
1581
Joerg Roedelccb50e02015-12-21 17:49:34 +01001582 if (flush) {
1583 domain_flush_tlb(&dom->domain);
1584 domain_flush_complete(&dom->domain);
1585 }
1586
Joerg Roedela0f51442015-12-21 16:20:09 +01001587 return address;
1588}
1589
Joerg Roedel384de722009-05-15 12:30:05 +02001590static unsigned long dma_ops_area_alloc(struct device *dev,
1591 struct dma_ops_domain *dom,
1592 unsigned int pages,
1593 unsigned long align_mask,
Joerg Roedel05ab49e2015-12-21 17:58:26 +01001594 u64 dma_mask)
Joerg Roedel384de722009-05-15 12:30:05 +02001595{
Joerg Roedelab7032b2015-12-21 18:47:11 +01001596 unsigned long boundary_size, mask;
Joerg Roedel384de722009-05-15 12:30:05 +02001597 unsigned long address = -1;
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001598 bool first = true;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001599 u32 start, i;
1600
1601 preempt_disable();
Joerg Roedel384de722009-05-15 12:30:05 +02001602
Joerg Roedele6aabee2015-05-27 09:26:09 +02001603 mask = dma_get_seg_boundary(dev);
1604
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001605again:
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001606 start = this_cpu_read(*dom->next_index);
1607
1608 /* Sanity check - is it really necessary? */
1609 if (unlikely(start > APERTURE_MAX_RANGES)) {
1610 start = 0;
1611 this_cpu_write(*dom->next_index, 0);
1612 }
1613
Joerg Roedele6aabee2015-05-27 09:26:09 +02001614 boundary_size = mask + 1 ? ALIGN(mask + 1, PAGE_SIZE) >> PAGE_SHIFT :
1615 1UL << (BITS_PER_LONG - PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001616
Joerg Roedel2a874422015-12-21 18:34:47 +01001617 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1618 struct aperture_range *range;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001619 int index;
Joerg Roedelccb50e02015-12-21 17:49:34 +01001620
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001621 index = (start + i) % APERTURE_MAX_RANGES;
1622
1623 range = dom->aperture[index];
Joerg Roedel2a874422015-12-21 18:34:47 +01001624
1625 if (!range || range->offset >= dma_mask)
1626 continue;
Joerg Roedel384de722009-05-15 12:30:05 +02001627
Joerg Roedel2a874422015-12-21 18:34:47 +01001628 address = dma_ops_aperture_alloc(dom, range, pages,
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001629 dma_mask, boundary_size,
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001630 align_mask, first);
Joerg Roedel384de722009-05-15 12:30:05 +02001631 if (address != -1) {
Joerg Roedel2a874422015-12-21 18:34:47 +01001632 address = range->offset + (address << PAGE_SHIFT);
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001633 this_cpu_write(*dom->next_index, index);
Joerg Roedel384de722009-05-15 12:30:05 +02001634 break;
1635 }
Joerg Roedel384de722009-05-15 12:30:05 +02001636 }
1637
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001638 if (address == -1 && first) {
1639 first = false;
1640 goto again;
1641 }
1642
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001643 preempt_enable();
1644
Joerg Roedel384de722009-05-15 12:30:05 +02001645 return address;
1646}
1647
Joerg Roedeld3086442008-06-26 21:27:57 +02001648static unsigned long dma_ops_alloc_addresses(struct device *dev,
1649 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001650 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001651 unsigned long align_mask,
1652 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001653{
Joerg Roedel266a3bd2015-12-21 18:54:24 +01001654 unsigned long address = -1;
Joerg Roedeld3086442008-06-26 21:27:57 +02001655
Joerg Roedel266a3bd2015-12-21 18:54:24 +01001656 while (address == -1) {
1657 address = dma_ops_area_alloc(dev, dom, pages,
1658 align_mask, dma_mask);
1659
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001660 if (address == -1 && alloc_new_range(dom, false, GFP_ATOMIC))
Joerg Roedel266a3bd2015-12-21 18:54:24 +01001661 break;
1662 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001663
Joerg Roedel384de722009-05-15 12:30:05 +02001664 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001665 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001666
1667 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1668
1669 return address;
1670}
1671
Joerg Roedel431b2a22008-07-11 17:14:22 +02001672/*
1673 * The address free function.
1674 *
1675 * called with domain->lock held
1676 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001677static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1678 unsigned long address,
1679 unsigned int pages)
1680{
Joerg Roedel384de722009-05-15 12:30:05 +02001681 unsigned i = address >> APERTURE_RANGE_SHIFT;
1682 struct aperture_range *range = dom->aperture[i];
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001683 unsigned long flags;
Joerg Roedel80be3082008-11-06 14:59:05 +01001684
Joerg Roedel384de722009-05-15 12:30:05 +02001685 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1686
Joerg Roedel47bccd62009-05-22 12:40:54 +02001687#ifdef CONFIG_IOMMU_STRESS
1688 if (i < 4)
1689 return;
1690#endif
1691
Joerg Roedel4eeca8c2015-12-22 12:15:35 +01001692 if (amd_iommu_unmap_flush) {
Joerg Roedeld41ab092015-12-21 18:20:03 +01001693 domain_flush_tlb(&dom->domain);
1694 domain_flush_complete(&dom->domain);
1695 }
Joerg Roedel384de722009-05-15 12:30:05 +02001696
1697 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001698
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001699 spin_lock_irqsave(&range->bitmap_lock, flags);
Joerg Roedel4eeca8c2015-12-22 12:15:35 +01001700 if (address + pages > range->next_bit)
1701 range->next_bit = address + pages;
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001702 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001703 spin_unlock_irqrestore(&range->bitmap_lock, flags);
Joerg Roedel384de722009-05-15 12:30:05 +02001704
Joerg Roedeld3086442008-06-26 21:27:57 +02001705}
1706
Joerg Roedel431b2a22008-07-11 17:14:22 +02001707/****************************************************************************
1708 *
1709 * The next functions belong to the domain allocation. A domain is
1710 * allocated for every IOMMU as the default domain. If device isolation
1711 * is enabled, every device get its own domain. The most important thing
1712 * about domains is the page table mapping the DMA address space they
1713 * contain.
1714 *
1715 ****************************************************************************/
1716
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001717/*
1718 * This function adds a protection domain to the global protection domain list
1719 */
1720static void add_domain_to_list(struct protection_domain *domain)
1721{
1722 unsigned long flags;
1723
1724 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1725 list_add(&domain->list, &amd_iommu_pd_list);
1726 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1727}
1728
1729/*
1730 * This function removes a protection domain to the global
1731 * protection domain list
1732 */
1733static void del_domain_from_list(struct protection_domain *domain)
1734{
1735 unsigned long flags;
1736
1737 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1738 list_del(&domain->list);
1739 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1740}
1741
Joerg Roedelec487d12008-06-26 21:27:58 +02001742static u16 domain_id_alloc(void)
1743{
1744 unsigned long flags;
1745 int id;
1746
1747 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1748 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1749 BUG_ON(id == 0);
1750 if (id > 0 && id < MAX_DOMAIN_ID)
1751 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1752 else
1753 id = 0;
1754 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1755
1756 return id;
1757}
1758
Joerg Roedela2acfb72008-12-02 18:28:53 +01001759static void domain_id_free(int id)
1760{
1761 unsigned long flags;
1762
1763 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1764 if (id > 0 && id < MAX_DOMAIN_ID)
1765 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1766 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1767}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001768
Joerg Roedel5c34c402013-06-20 20:22:58 +02001769#define DEFINE_FREE_PT_FN(LVL, FN) \
1770static void free_pt_##LVL (unsigned long __pt) \
1771{ \
1772 unsigned long p; \
1773 u64 *pt; \
1774 int i; \
1775 \
1776 pt = (u64 *)__pt; \
1777 \
1778 for (i = 0; i < 512; ++i) { \
Joerg Roedel0b3fff542015-06-18 10:48:34 +02001779 /* PTE present? */ \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001780 if (!IOMMU_PTE_PRESENT(pt[i])) \
1781 continue; \
1782 \
Joerg Roedel0b3fff542015-06-18 10:48:34 +02001783 /* Large PTE? */ \
1784 if (PM_PTE_LEVEL(pt[i]) == 0 || \
1785 PM_PTE_LEVEL(pt[i]) == 7) \
1786 continue; \
1787 \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001788 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1789 FN(p); \
1790 } \
1791 free_page((unsigned long)pt); \
1792}
1793
1794DEFINE_FREE_PT_FN(l2, free_page)
1795DEFINE_FREE_PT_FN(l3, free_pt_l2)
1796DEFINE_FREE_PT_FN(l4, free_pt_l3)
1797DEFINE_FREE_PT_FN(l5, free_pt_l4)
1798DEFINE_FREE_PT_FN(l6, free_pt_l5)
1799
Joerg Roedel86db2e52008-12-02 18:20:21 +01001800static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001801{
Joerg Roedel5c34c402013-06-20 20:22:58 +02001802 unsigned long root = (unsigned long)domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001803
Joerg Roedel5c34c402013-06-20 20:22:58 +02001804 switch (domain->mode) {
1805 case PAGE_MODE_NONE:
1806 break;
1807 case PAGE_MODE_1_LEVEL:
1808 free_page(root);
1809 break;
1810 case PAGE_MODE_2_LEVEL:
1811 free_pt_l2(root);
1812 break;
1813 case PAGE_MODE_3_LEVEL:
1814 free_pt_l3(root);
1815 break;
1816 case PAGE_MODE_4_LEVEL:
1817 free_pt_l4(root);
1818 break;
1819 case PAGE_MODE_5_LEVEL:
1820 free_pt_l5(root);
1821 break;
1822 case PAGE_MODE_6_LEVEL:
1823 free_pt_l6(root);
1824 break;
1825 default:
1826 BUG();
Joerg Roedelec487d12008-06-26 21:27:58 +02001827 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001828}
1829
Joerg Roedelb16137b2011-11-21 16:50:23 +01001830static void free_gcr3_tbl_level1(u64 *tbl)
1831{
1832 u64 *ptr;
1833 int i;
1834
1835 for (i = 0; i < 512; ++i) {
1836 if (!(tbl[i] & GCR3_VALID))
1837 continue;
1838
1839 ptr = __va(tbl[i] & PAGE_MASK);
1840
1841 free_page((unsigned long)ptr);
1842 }
1843}
1844
1845static void free_gcr3_tbl_level2(u64 *tbl)
1846{
1847 u64 *ptr;
1848 int i;
1849
1850 for (i = 0; i < 512; ++i) {
1851 if (!(tbl[i] & GCR3_VALID))
1852 continue;
1853
1854 ptr = __va(tbl[i] & PAGE_MASK);
1855
1856 free_gcr3_tbl_level1(ptr);
1857 }
1858}
1859
Joerg Roedel52815b72011-11-17 17:24:28 +01001860static void free_gcr3_table(struct protection_domain *domain)
1861{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001862 if (domain->glx == 2)
1863 free_gcr3_tbl_level2(domain->gcr3_tbl);
1864 else if (domain->glx == 1)
1865 free_gcr3_tbl_level1(domain->gcr3_tbl);
Joerg Roedel23d3a982015-08-13 11:15:13 +02001866 else
1867 BUG_ON(domain->glx != 0);
Joerg Roedelb16137b2011-11-21 16:50:23 +01001868
Joerg Roedel52815b72011-11-17 17:24:28 +01001869 free_page((unsigned long)domain->gcr3_tbl);
1870}
1871
Joerg Roedel431b2a22008-07-11 17:14:22 +02001872/*
1873 * Free a domain, only used if something went wrong in the
1874 * allocation path and we need to free an already allocated page table
1875 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001876static void dma_ops_domain_free(struct dma_ops_domain *dom)
1877{
Joerg Roedel384de722009-05-15 12:30:05 +02001878 int i;
1879
Joerg Roedelec487d12008-06-26 21:27:58 +02001880 if (!dom)
1881 return;
1882
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001883 free_percpu(dom->next_index);
1884
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001885 del_domain_from_list(&dom->domain);
1886
Joerg Roedel86db2e52008-12-02 18:20:21 +01001887 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001888
Joerg Roedel384de722009-05-15 12:30:05 +02001889 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1890 if (!dom->aperture[i])
1891 continue;
1892 free_page((unsigned long)dom->aperture[i]->bitmap);
1893 kfree(dom->aperture[i]);
1894 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001895
1896 kfree(dom);
1897}
1898
Joerg Roedela639a8e2015-12-22 16:06:49 +01001899static int dma_ops_domain_alloc_apertures(struct dma_ops_domain *dma_dom,
1900 int max_apertures)
1901{
1902 int ret, i, apertures;
1903
1904 apertures = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
1905 ret = 0;
1906
1907 for (i = apertures; i < max_apertures; ++i) {
1908 ret = alloc_new_range(dma_dom, false, GFP_KERNEL);
1909 if (ret)
1910 break;
1911 }
1912
1913 return ret;
1914}
1915
Joerg Roedel431b2a22008-07-11 17:14:22 +02001916/*
1917 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001918 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001919 * structures required for the dma_ops interface
1920 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001921static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001922{
1923 struct dma_ops_domain *dma_dom;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001924 int cpu;
Joerg Roedelec487d12008-06-26 21:27:58 +02001925
1926 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1927 if (!dma_dom)
1928 return NULL;
1929
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001930 if (protection_domain_init(&dma_dom->domain))
Joerg Roedelec487d12008-06-26 21:27:58 +02001931 goto free_dma_dom;
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001932
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001933 dma_dom->next_index = alloc_percpu(u32);
1934 if (!dma_dom->next_index)
1935 goto free_dma_dom;
1936
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001937 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001938 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001939 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001940 dma_dom->domain.priv = dma_dom;
1941 if (!dma_dom->domain.pt_root)
1942 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001943
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001944 add_domain_to_list(&dma_dom->domain);
1945
Joerg Roedel576175c2009-11-23 19:08:46 +01001946 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001947 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001948
Joerg Roedel431b2a22008-07-11 17:14:22 +02001949 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001950 * mark the first page as allocated so we never return 0 as
1951 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001952 */
Joerg Roedel384de722009-05-15 12:30:05 +02001953 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedelec487d12008-06-26 21:27:58 +02001954
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001955 for_each_possible_cpu(cpu)
1956 *per_cpu_ptr(dma_dom->next_index, cpu) = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001957
1958 return dma_dom;
1959
1960free_dma_dom:
1961 dma_ops_domain_free(dma_dom);
1962
1963 return NULL;
1964}
1965
Joerg Roedel431b2a22008-07-11 17:14:22 +02001966/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001967 * little helper function to check whether a given protection domain is a
1968 * dma_ops domain
1969 */
1970static bool dma_ops_domain(struct protection_domain *domain)
1971{
1972 return domain->flags & PD_DMA_OPS_MASK;
1973}
1974
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001975static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001976{
Joerg Roedel132bd682011-11-17 14:18:46 +01001977 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001978 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001979
Joerg Roedel132bd682011-11-17 14:18:46 +01001980 if (domain->mode != PAGE_MODE_NONE)
1981 pte_root = virt_to_phys(domain->pt_root);
1982
Joerg Roedel38ddf412008-09-11 10:38:32 +02001983 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1984 << DEV_ENTRY_MODE_SHIFT;
1985 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001986
Joerg Roedelee6c2862011-11-09 12:06:03 +01001987 flags = amd_iommu_dev_table[devid].data[1];
1988
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001989 if (ats)
1990 flags |= DTE_FLAG_IOTLB;
1991
Joerg Roedel52815b72011-11-17 17:24:28 +01001992 if (domain->flags & PD_IOMMUV2_MASK) {
1993 u64 gcr3 = __pa(domain->gcr3_tbl);
1994 u64 glx = domain->glx;
1995 u64 tmp;
1996
1997 pte_root |= DTE_FLAG_GV;
1998 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1999
2000 /* First mask out possible old values for GCR3 table */
2001 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
2002 flags &= ~tmp;
2003
2004 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
2005 flags &= ~tmp;
2006
2007 /* Encode GCR3 table into DTE */
2008 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
2009 pte_root |= tmp;
2010
2011 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
2012 flags |= tmp;
2013
2014 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
2015 flags |= tmp;
2016 }
2017
Joerg Roedelee6c2862011-11-09 12:06:03 +01002018 flags &= ~(0xffffUL);
2019 flags |= domain->id;
2020
2021 amd_iommu_dev_table[devid].data[1] = flags;
2022 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002023}
2024
Joerg Roedel15898bb2009-11-24 15:39:42 +01002025static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002026{
Joerg Roedel355bf552008-12-08 12:02:41 +01002027 /* remove entry from the device table seen by the hardware */
Joerg Roedelcbf3ccd2015-10-20 14:59:36 +02002028 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2029 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
Joerg Roedel355bf552008-12-08 12:02:41 +01002030
Joerg Roedelc5cca142009-10-09 18:31:20 +02002031 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002032}
2033
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002034static void do_attach(struct iommu_dev_data *dev_data,
2035 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002036{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002037 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02002038 u16 alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002039 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002040
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002041 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele25bfb52015-10-20 17:33:38 +02002042 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002043 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002044
2045 /* Update data structures */
2046 dev_data->domain = domain;
2047 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002048
2049 /* Do reference counting */
2050 domain->dev_iommu[iommu->index] += 1;
2051 domain->dev_cnt += 1;
2052
Joerg Roedele25bfb52015-10-20 17:33:38 +02002053 /* Update device table */
2054 set_dte_entry(dev_data->devid, domain, ats);
2055 if (alias != dev_data->devid)
Baoquan He9b1a12d2016-01-20 22:01:19 +08002056 set_dte_entry(alias, domain, ats);
Joerg Roedele25bfb52015-10-20 17:33:38 +02002057
Joerg Roedel6c542042011-06-09 17:07:31 +02002058 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002059}
2060
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002061static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002062{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002063 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02002064 u16 alias;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002065
Joerg Roedel5adad992015-10-09 16:23:33 +02002066 /*
2067 * First check if the device is still attached. It might already
2068 * be detached from its domain because the generic
2069 * iommu_detach_group code detached it and we try again here in
2070 * our alias handling.
2071 */
2072 if (!dev_data->domain)
2073 return;
2074
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002075 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele25bfb52015-10-20 17:33:38 +02002076 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002077
Joerg Roedelc4596112009-11-20 14:57:32 +01002078 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002079 dev_data->domain->dev_iommu[iommu->index] -= 1;
2080 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002081
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002082 /* Update data structures */
2083 dev_data->domain = NULL;
2084 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002085 clear_dte_entry(dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02002086 if (alias != dev_data->devid)
2087 clear_dte_entry(alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002088
2089 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002090 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002091}
2092
2093/*
2094 * If a device is not yet associated with a domain, this function does
2095 * assigns it visible for the hardware
2096 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002097static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002098 struct protection_domain *domain)
2099{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002100 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002101
Joerg Roedel272e4f92015-10-20 17:33:37 +02002102 /*
2103 * Must be called with IRQs disabled. Warn here to detect early
2104 * when its not.
2105 */
2106 WARN_ON(!irqs_disabled());
2107
Joerg Roedel15898bb2009-11-24 15:39:42 +01002108 /* lock domain */
2109 spin_lock(&domain->lock);
2110
Joerg Roedel397111a2014-08-05 17:31:51 +02002111 ret = -EBUSY;
Joerg Roedel150952f2015-10-20 17:33:35 +02002112 if (dev_data->domain != NULL)
Joerg Roedel397111a2014-08-05 17:31:51 +02002113 goto out_unlock;
Joerg Roedel24100052009-11-25 15:59:57 +01002114
Joerg Roedel397111a2014-08-05 17:31:51 +02002115 /* Attach alias group root */
Joerg Roedel150952f2015-10-20 17:33:35 +02002116 do_attach(dev_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002117
Julia Lawall84fe6c12010-05-27 12:31:51 +02002118 ret = 0;
2119
2120out_unlock:
2121
Joerg Roedel355bf552008-12-08 12:02:41 +01002122 /* ready */
2123 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002124
Julia Lawall84fe6c12010-05-27 12:31:51 +02002125 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002126}
2127
Joerg Roedel52815b72011-11-17 17:24:28 +01002128
2129static void pdev_iommuv2_disable(struct pci_dev *pdev)
2130{
2131 pci_disable_ats(pdev);
2132 pci_disable_pri(pdev);
2133 pci_disable_pasid(pdev);
2134}
2135
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002136/* FIXME: Change generic reset-function to do the same */
2137static int pri_reset_while_enabled(struct pci_dev *pdev)
2138{
2139 u16 control;
2140 int pos;
2141
Joerg Roedel46277b72011-12-07 14:34:02 +01002142 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002143 if (!pos)
2144 return -EINVAL;
2145
Joerg Roedel46277b72011-12-07 14:34:02 +01002146 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2147 control |= PCI_PRI_CTRL_RESET;
2148 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002149
2150 return 0;
2151}
2152
Joerg Roedel52815b72011-11-17 17:24:28 +01002153static int pdev_iommuv2_enable(struct pci_dev *pdev)
2154{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002155 bool reset_enable;
2156 int reqs, ret;
2157
2158 /* FIXME: Hardcode number of outstanding requests for now */
2159 reqs = 32;
2160 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2161 reqs = 1;
2162 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002163
2164 /* Only allow access to user-accessible pages */
2165 ret = pci_enable_pasid(pdev, 0);
2166 if (ret)
2167 goto out_err;
2168
2169 /* First reset the PRI state of the device */
2170 ret = pci_reset_pri(pdev);
2171 if (ret)
2172 goto out_err;
2173
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002174 /* Enable PRI */
2175 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002176 if (ret)
2177 goto out_err;
2178
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002179 if (reset_enable) {
2180 ret = pri_reset_while_enabled(pdev);
2181 if (ret)
2182 goto out_err;
2183 }
2184
Joerg Roedel52815b72011-11-17 17:24:28 +01002185 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2186 if (ret)
2187 goto out_err;
2188
2189 return 0;
2190
2191out_err:
2192 pci_disable_pri(pdev);
2193 pci_disable_pasid(pdev);
2194
2195 return ret;
2196}
2197
Joerg Roedelc99afa22011-11-21 18:19:25 +01002198/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002199#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002200
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002201static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002202{
Joerg Roedela3b93122012-04-12 12:49:26 +02002203 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002204 int pos;
2205
Joerg Roedel46277b72011-12-07 14:34:02 +01002206 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002207 if (!pos)
2208 return false;
2209
Joerg Roedela3b93122012-04-12 12:49:26 +02002210 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002211
Joerg Roedela3b93122012-04-12 12:49:26 +02002212 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002213}
2214
Joerg Roedel15898bb2009-11-24 15:39:42 +01002215/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002216 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002217 * assigns it visible for the hardware
2218 */
2219static int attach_device(struct device *dev,
2220 struct protection_domain *domain)
2221{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002222 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002223 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002224 unsigned long flags;
2225 int ret;
2226
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002227 dev_data = get_dev_data(dev);
2228
Joerg Roedel52815b72011-11-17 17:24:28 +01002229 if (domain->flags & PD_IOMMUV2_MASK) {
Joerg Roedel02ca2022015-07-28 16:58:49 +02002230 if (!dev_data->passthrough)
Joerg Roedel52815b72011-11-17 17:24:28 +01002231 return -EINVAL;
2232
Joerg Roedel02ca2022015-07-28 16:58:49 +02002233 if (dev_data->iommu_v2) {
2234 if (pdev_iommuv2_enable(pdev) != 0)
2235 return -EINVAL;
Joerg Roedel52815b72011-11-17 17:24:28 +01002236
Joerg Roedel02ca2022015-07-28 16:58:49 +02002237 dev_data->ats.enabled = true;
2238 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2239 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
2240 }
Joerg Roedel52815b72011-11-17 17:24:28 +01002241 } else if (amd_iommu_iotlb_sup &&
2242 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002243 dev_data->ats.enabled = true;
2244 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2245 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002246
Joerg Roedel15898bb2009-11-24 15:39:42 +01002247 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002248 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002249 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2250
2251 /*
2252 * We might boot into a crash-kernel here. The crashed kernel
2253 * left the caches in the IOMMU dirty. So we have to flush
2254 * here to evict all dirty stuff.
2255 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002256 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002257
2258 return ret;
2259}
2260
2261/*
2262 * Removes a device from a protection domain (unlocked)
2263 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002264static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002265{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002266 struct protection_domain *domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002267
Joerg Roedel272e4f92015-10-20 17:33:37 +02002268 /*
2269 * Must be called with IRQs disabled. Warn here to detect early
2270 * when its not.
2271 */
2272 WARN_ON(!irqs_disabled());
2273
Joerg Roedelf34c73f2015-10-20 17:33:34 +02002274 if (WARN_ON(!dev_data->domain))
2275 return;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002276
Joerg Roedel2ca76272010-01-22 16:45:31 +01002277 domain = dev_data->domain;
2278
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002279 spin_lock(&domain->lock);
Joerg Roedel24100052009-11-25 15:59:57 +01002280
Joerg Roedel150952f2015-10-20 17:33:35 +02002281 do_detach(dev_data);
Joerg Roedel71f77582011-06-09 19:03:15 +02002282
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002283 spin_unlock(&domain->lock);
Joerg Roedel355bf552008-12-08 12:02:41 +01002284}
2285
2286/*
2287 * Removes a device from a protection domain (with devtable_lock held)
2288 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002289static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002290{
Joerg Roedel52815b72011-11-17 17:24:28 +01002291 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002292 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002293 unsigned long flags;
2294
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002295 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002296 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002297
Joerg Roedel355bf552008-12-08 12:02:41 +01002298 /* lock device table */
2299 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002300 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002301 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002302
Joerg Roedel02ca2022015-07-28 16:58:49 +02002303 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
Joerg Roedel52815b72011-11-17 17:24:28 +01002304 pdev_iommuv2_disable(to_pci_dev(dev));
2305 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002306 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002307
2308 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002309}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002310
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002311static int amd_iommu_add_device(struct device *dev)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002312{
Joerg Roedel71f77582011-06-09 19:03:15 +02002313 struct iommu_dev_data *dev_data;
Joerg Roedel07ee8692015-05-28 18:41:42 +02002314 struct iommu_domain *domain;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002315 struct amd_iommu *iommu;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002316 u16 devid;
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002317 int ret;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002318
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002319 if (!check_device(dev) || get_dev_data(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002320 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002321
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002322 devid = get_device_id(dev);
2323 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01002324
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002325 ret = iommu_init_device(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002326 if (ret) {
2327 if (ret != -ENOTSUPP)
2328 pr_err("Failed to initialize device %s - trying to proceed anyway\n",
2329 dev_name(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +01002330
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002331 iommu_ignore_device(dev);
Joerg Roedel343e9ca2015-05-28 18:41:43 +02002332 dev->archdata.dma_ops = &nommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002333 goto out;
2334 }
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002335 init_iommu_group(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002336
Joerg Roedel07ee8692015-05-28 18:41:42 +02002337 dev_data = get_dev_data(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002338
2339 BUG_ON(!dev_data);
2340
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002341 if (iommu_pass_through || dev_data->iommu_v2)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002342 iommu_request_dm_for_dev(dev);
2343
2344 /* Domains are initialized for this device - have a look what we ended up with */
2345 domain = iommu_get_domain_for_dev(dev);
Joerg Roedel32302322015-07-28 16:58:50 +02002346 if (domain->type == IOMMU_DOMAIN_IDENTITY)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002347 dev_data->passthrough = true;
Joerg Roedel32302322015-07-28 16:58:50 +02002348 else
Joerg Roedel07ee8692015-05-28 18:41:42 +02002349 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002350
2351out:
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002352 iommu_completion_wait(iommu);
2353
Joerg Roedele275a2a2008-12-10 18:27:25 +01002354 return 0;
2355}
2356
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002357static void amd_iommu_remove_device(struct device *dev)
Joerg Roedel8638c492009-12-10 11:12:25 +01002358{
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002359 struct amd_iommu *iommu;
2360 u16 devid;
2361
2362 if (!check_device(dev))
2363 return;
2364
2365 devid = get_device_id(dev);
2366 iommu = amd_iommu_rlookup_table[devid];
2367
2368 iommu_uninit_device(dev);
2369 iommu_completion_wait(iommu);
Joerg Roedel8638c492009-12-10 11:12:25 +01002370}
2371
Joerg Roedel431b2a22008-07-11 17:14:22 +02002372/*****************************************************************************
2373 *
2374 * The next functions belong to the dma_ops mapping/unmapping code.
2375 *
2376 *****************************************************************************/
2377
2378/*
2379 * In the dma_ops path we only have the struct device. This function
2380 * finds the corresponding IOMMU, the protection domain and the
2381 * requestor id for a given device.
2382 * If the device is not yet associated with a domain this is also done
2383 * in this function.
2384 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002385static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002386{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002387 struct protection_domain *domain;
Joerg Roedel063071d2015-05-28 18:41:38 +02002388 struct iommu_domain *io_domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002389
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002390 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002391 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002392
Joerg Roedel063071d2015-05-28 18:41:38 +02002393 io_domain = iommu_get_domain_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002394 if (!io_domain)
2395 return NULL;
Joerg Roedel063071d2015-05-28 18:41:38 +02002396
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002397 domain = to_pdomain(io_domain);
2398 if (!dma_ops_domain(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002399 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002400
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002401 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002402}
2403
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002404static void update_device_table(struct protection_domain *domain)
2405{
Joerg Roedel492667d2009-11-27 13:25:47 +01002406 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002407
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002408 list_for_each_entry(dev_data, &domain->dev_list, list)
2409 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002410}
2411
2412static void update_domain(struct protection_domain *domain)
2413{
2414 if (!domain->updated)
2415 return;
2416
2417 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002418
2419 domain_flush_devices(domain);
2420 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002421
2422 domain->updated = false;
2423}
2424
Joerg Roedel431b2a22008-07-11 17:14:22 +02002425/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002426 * This function fetches the PTE for a given address in the aperture
2427 */
2428static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2429 unsigned long address)
2430{
Joerg Roedel384de722009-05-15 12:30:05 +02002431 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002432 u64 *pte, *pte_page;
2433
Joerg Roedel384de722009-05-15 12:30:05 +02002434 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2435 if (!aperture)
2436 return NULL;
2437
2438 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002439 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002440 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002441 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002442 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2443 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002444 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002445
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002446 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002447
2448 return pte;
2449}
2450
2451/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002452 * This is the generic map function. It maps one 4kb page at paddr to
2453 * the given address in the DMA address space for the domain.
2454 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002455static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002456 unsigned long address,
2457 phys_addr_t paddr,
2458 int direction)
2459{
2460 u64 *pte, __pte;
2461
2462 WARN_ON(address > dom->aperture_size);
2463
2464 paddr &= PAGE_MASK;
2465
Joerg Roedel8bda3092009-05-12 12:02:46 +02002466 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002467 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002468 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002469
2470 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2471
2472 if (direction == DMA_TO_DEVICE)
2473 __pte |= IOMMU_PTE_IR;
2474 else if (direction == DMA_FROM_DEVICE)
2475 __pte |= IOMMU_PTE_IW;
2476 else if (direction == DMA_BIDIRECTIONAL)
2477 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2478
Joerg Roedela7fb6682015-12-21 12:50:54 +01002479 WARN_ON_ONCE(*pte);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002480
2481 *pte = __pte;
2482
2483 return (dma_addr_t)address;
2484}
2485
Joerg Roedel431b2a22008-07-11 17:14:22 +02002486/*
2487 * The generic unmapping function for on page in the DMA address space.
2488 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002489static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002490 unsigned long address)
2491{
Joerg Roedel384de722009-05-15 12:30:05 +02002492 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002493 u64 *pte;
2494
2495 if (address >= dom->aperture_size)
2496 return;
2497
Joerg Roedel384de722009-05-15 12:30:05 +02002498 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2499 if (!aperture)
2500 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002501
Joerg Roedel384de722009-05-15 12:30:05 +02002502 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2503 if (!pte)
2504 return;
2505
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002506 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002507
Joerg Roedela7fb6682015-12-21 12:50:54 +01002508 WARN_ON_ONCE(!*pte);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002509
2510 *pte = 0ULL;
2511}
2512
Joerg Roedel431b2a22008-07-11 17:14:22 +02002513/*
2514 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002515 * contiguous memory region into DMA address space. It is used by all
2516 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002517 * Must be called with the domain lock held.
2518 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002519static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002520 struct dma_ops_domain *dma_dom,
2521 phys_addr_t paddr,
2522 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002523 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002524 bool align,
2525 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002526{
2527 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002528 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002529 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002530 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002531 int i;
2532
Joerg Roedele3c449f2008-10-15 22:02:11 -07002533 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002534 paddr &= PAGE_MASK;
2535
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002536 INC_STATS_COUNTER(total_map_requests);
2537
Joerg Roedelc1858972008-12-12 15:42:39 +01002538 if (pages > 1)
2539 INC_STATS_COUNTER(cross_page);
2540
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002541 if (align)
2542 align_mask = (1UL << get_order(size)) - 1;
2543
Joerg Roedel832a90c2008-09-18 15:54:23 +02002544 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2545 dma_mask);
Joerg Roedelebaecb42015-12-21 18:11:32 +01002546
Joerg Roedel266a3bd2015-12-21 18:54:24 +01002547 if (address == DMA_ERROR_CODE)
2548 goto out;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002549
2550 start = address;
2551 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002552 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002553 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002554 goto out_unmap;
2555
Joerg Roedelcb76c322008-06-26 21:28:00 +02002556 paddr += PAGE_SIZE;
2557 start += PAGE_SIZE;
2558 }
2559 address += offset;
2560
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002561 ADD_STATS_COUNTER(alloced_io_mem, size);
2562
Joerg Roedelab7032b2015-12-21 18:47:11 +01002563 if (unlikely(amd_iommu_np_cache)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002564 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedelab7032b2015-12-21 18:47:11 +01002565 domain_flush_complete(&dma_dom->domain);
2566 }
Joerg Roedel270cab242008-09-04 15:49:46 +02002567
Joerg Roedelcb76c322008-06-26 21:28:00 +02002568out:
2569 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002570
2571out_unmap:
2572
2573 for (--i; i >= 0; --i) {
2574 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002575 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002576 }
2577
2578 dma_ops_free_addresses(dma_dom, address, pages);
2579
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002580 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002581}
2582
Joerg Roedel431b2a22008-07-11 17:14:22 +02002583/*
2584 * Does the reverse of the __map_single function. Must be called with
2585 * the domain lock held too
2586 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002587static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002588 dma_addr_t dma_addr,
2589 size_t size,
2590 int dir)
2591{
Joerg Roedel04e04632010-09-23 16:12:48 +02002592 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002593 dma_addr_t i, start;
2594 unsigned int pages;
2595
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002596 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002597 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002598 return;
2599
Joerg Roedel04e04632010-09-23 16:12:48 +02002600 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002601 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002602 dma_addr &= PAGE_MASK;
2603 start = dma_addr;
2604
2605 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002606 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002607 start += PAGE_SIZE;
2608 }
2609
Joerg Roedel84b3a0b2015-12-21 13:23:59 +01002610 SUB_STATS_COUNTER(alloced_io_mem, size);
2611
2612 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002613}
2614
Joerg Roedel431b2a22008-07-11 17:14:22 +02002615/*
2616 * The exported map_single function for dma_ops.
2617 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002618static dma_addr_t map_page(struct device *dev, struct page *page,
2619 unsigned long offset, size_t size,
2620 enum dma_data_direction dir,
2621 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002622{
FUJITA Tomonori51491362009-01-05 23:47:25 +09002623 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002624 struct protection_domain *domain;
2625 u64 dma_mask;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002626
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002627 INC_STATS_COUNTER(cnt_map_single);
2628
Joerg Roedel94f6d192009-11-24 16:40:02 +01002629 domain = get_domain(dev);
2630 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002631 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002632 else if (IS_ERR(domain))
2633 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002634
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002635 dma_mask = *dev->dma_mask;
2636
Joerg Roedel92d420e2015-12-21 19:31:33 +01002637 return __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002638 dma_mask);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002639}
2640
Joerg Roedel431b2a22008-07-11 17:14:22 +02002641/*
2642 * The exported unmap_single function for dma_ops.
2643 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002644static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2645 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002646{
Joerg Roedel4da70b92008-06-26 21:28:01 +02002647 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002648
Joerg Roedel146a6912008-12-12 15:07:12 +01002649 INC_STATS_COUNTER(cnt_unmap_single);
2650
Joerg Roedel94f6d192009-11-24 16:40:02 +01002651 domain = get_domain(dev);
2652 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002653 return;
2654
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002655 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002656}
2657
Joerg Roedel431b2a22008-07-11 17:14:22 +02002658/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002659 * The exported map_sg function for dma_ops (handles scatter-gather
2660 * lists).
2661 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002662static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002663 int nelems, enum dma_data_direction dir,
2664 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002665{
Joerg Roedel65b050a2008-06-26 21:28:02 +02002666 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002667 int i;
2668 struct scatterlist *s;
2669 phys_addr_t paddr;
2670 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002671 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002672
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002673 INC_STATS_COUNTER(cnt_map_sg);
2674
Joerg Roedel94f6d192009-11-24 16:40:02 +01002675 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002676 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002677 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002678
Joerg Roedel832a90c2008-09-18 15:54:23 +02002679 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002680
Joerg Roedel65b050a2008-06-26 21:28:02 +02002681 for_each_sg(sglist, s, nelems, i) {
2682 paddr = sg_phys(s);
2683
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002684 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002685 paddr, s->length, dir, false,
2686 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002687
2688 if (s->dma_address) {
2689 s->dma_length = s->length;
2690 mapped_elems++;
2691 } else
2692 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002693 }
2694
Joerg Roedel65b050a2008-06-26 21:28:02 +02002695 return mapped_elems;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002696
Joerg Roedel65b050a2008-06-26 21:28:02 +02002697unmap:
2698 for_each_sg(sglist, s, mapped_elems, i) {
2699 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002700 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002701 s->dma_length, dir);
2702 s->dma_address = s->dma_length = 0;
2703 }
2704
Joerg Roedel92d420e2015-12-21 19:31:33 +01002705 return 0;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002706}
2707
Joerg Roedel431b2a22008-07-11 17:14:22 +02002708/*
2709 * The exported map_sg function for dma_ops (handles scatter-gather
2710 * lists).
2711 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002712static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002713 int nelems, enum dma_data_direction dir,
2714 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002715{
Joerg Roedel65b050a2008-06-26 21:28:02 +02002716 struct protection_domain *domain;
2717 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002718 int i;
2719
Joerg Roedel55877a62008-12-12 15:12:14 +01002720 INC_STATS_COUNTER(cnt_unmap_sg);
2721
Joerg Roedel94f6d192009-11-24 16:40:02 +01002722 domain = get_domain(dev);
2723 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002724 return;
2725
Joerg Roedel65b050a2008-06-26 21:28:02 +02002726 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002727 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002728 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002729 s->dma_address = s->dma_length = 0;
2730 }
Joerg Roedel65b050a2008-06-26 21:28:02 +02002731}
2732
Joerg Roedel431b2a22008-07-11 17:14:22 +02002733/*
2734 * The exported alloc_coherent function for dma_ops.
2735 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002736static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002737 dma_addr_t *dma_addr, gfp_t flag,
2738 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002739{
Joerg Roedel832a90c2008-09-18 15:54:23 +02002740 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002741 struct protection_domain *domain;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002742 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002743
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002744 INC_STATS_COUNTER(cnt_alloc_coherent);
2745
Joerg Roedel94f6d192009-11-24 16:40:02 +01002746 domain = get_domain(dev);
2747 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedel3b839a52015-04-01 14:58:47 +02002748 page = alloc_pages(flag, get_order(size));
2749 *dma_addr = page_to_phys(page);
2750 return page_address(page);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002751 } else if (IS_ERR(domain))
2752 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002753
Joerg Roedel3b839a52015-04-01 14:58:47 +02002754 size = PAGE_ALIGN(size);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002755 dma_mask = dev->coherent_dma_mask;
2756 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
Joerg Roedel2d0ec7a2015-06-01 17:30:57 +02002757 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002758
Joerg Roedel3b839a52015-04-01 14:58:47 +02002759 page = alloc_pages(flag | __GFP_NOWARN, get_order(size));
2760 if (!page) {
Mel Gormand0164ad2015-11-06 16:28:21 -08002761 if (!gfpflags_allow_blocking(flag))
Joerg Roedel3b839a52015-04-01 14:58:47 +02002762 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002763
Joerg Roedel3b839a52015-04-01 14:58:47 +02002764 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
2765 get_order(size));
2766 if (!page)
2767 return NULL;
2768 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002769
Joerg Roedel832a90c2008-09-18 15:54:23 +02002770 if (!dma_mask)
2771 dma_mask = *dev->dma_mask;
2772
Joerg Roedel3b839a52015-04-01 14:58:47 +02002773 *dma_addr = __map_single(dev, domain->priv, page_to_phys(page),
Joerg Roedel832a90c2008-09-18 15:54:23 +02002774 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002775
Joerg Roedel92d420e2015-12-21 19:31:33 +01002776 if (*dma_addr == DMA_ERROR_CODE)
Joerg Roedel5b28df62008-12-02 17:49:42 +01002777 goto out_free;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002778
Joerg Roedel3b839a52015-04-01 14:58:47 +02002779 return page_address(page);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002780
2781out_free:
2782
Joerg Roedel3b839a52015-04-01 14:58:47 +02002783 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2784 __free_pages(page, get_order(size));
Joerg Roedel5b28df62008-12-02 17:49:42 +01002785
2786 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002787}
2788
Joerg Roedel431b2a22008-07-11 17:14:22 +02002789/*
2790 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002791 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002792static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002793 void *virt_addr, dma_addr_t dma_addr,
2794 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002795{
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002796 struct protection_domain *domain;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002797 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002798
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002799 INC_STATS_COUNTER(cnt_free_coherent);
2800
Joerg Roedel3b839a52015-04-01 14:58:47 +02002801 page = virt_to_page(virt_addr);
2802 size = PAGE_ALIGN(size);
2803
Joerg Roedel94f6d192009-11-24 16:40:02 +01002804 domain = get_domain(dev);
2805 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002806 goto free_mem;
2807
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002808 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002809
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002810free_mem:
Joerg Roedel3b839a52015-04-01 14:58:47 +02002811 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2812 __free_pages(page, get_order(size));
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002813}
2814
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002815/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002816 * This function is called by the DMA layer to find out if we can handle a
2817 * particular device. It is part of the dma_ops.
2818 */
2819static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2820{
Joerg Roedel420aef82009-11-23 16:14:57 +01002821 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002822}
2823
Joerg Roedela639a8e2015-12-22 16:06:49 +01002824static int set_dma_mask(struct device *dev, u64 mask)
2825{
2826 struct protection_domain *domain;
2827 int max_apertures = 1;
2828
2829 domain = get_domain(dev);
2830 if (IS_ERR(domain))
2831 return PTR_ERR(domain);
2832
2833 if (mask == DMA_BIT_MASK(64))
2834 max_apertures = 8;
2835 else if (mask > DMA_BIT_MASK(32))
2836 max_apertures = 4;
2837
2838 /*
2839 * To prevent lock contention it doesn't make sense to allocate more
2840 * apertures than online cpus
2841 */
2842 if (max_apertures > num_online_cpus())
2843 max_apertures = num_online_cpus();
2844
2845 if (dma_ops_domain_alloc_apertures(domain->priv, max_apertures))
2846 dev_err(dev, "Can't allocate %d iommu apertures\n",
2847 max_apertures);
2848
2849 return 0;
2850}
2851
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002852static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedela639a8e2015-12-22 16:06:49 +01002853 .alloc = alloc_coherent,
2854 .free = free_coherent,
2855 .map_page = map_page,
2856 .unmap_page = unmap_page,
2857 .map_sg = map_sg,
2858 .unmap_sg = unmap_sg,
2859 .dma_supported = amd_iommu_dma_supported,
2860 .set_dma_mask = set_dma_mask,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002861};
2862
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002863int __init amd_iommu_init_api(void)
Joerg Roedel27c21272011-05-30 15:56:24 +02002864{
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002865 return bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01002866}
2867
Joerg Roedel6631ee92008-06-26 21:28:05 +02002868int __init amd_iommu_init_dma_ops(void)
2869{
Joerg Roedel32302322015-07-28 16:58:50 +02002870 swiotlb = iommu_pass_through ? 1 : 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002871 iommu_detected = 1;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002872
Joerg Roedel52717822015-07-28 16:58:51 +02002873 /*
2874 * In case we don't initialize SWIOTLB (actually the common case
2875 * when AMD IOMMU is enabled), make sure there are global
2876 * dma_ops set as a fall-back for devices not handled by this
2877 * driver (for example non-PCI devices).
2878 */
2879 if (!swiotlb)
2880 dma_ops = &nommu_dma_ops;
2881
Joerg Roedel7f265082008-12-12 13:50:21 +01002882 amd_iommu_stats_init();
2883
Joerg Roedel62410ee2012-06-12 16:42:43 +02002884 if (amd_iommu_unmap_flush)
2885 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
2886 else
2887 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
2888
Joerg Roedel6631ee92008-06-26 21:28:05 +02002889 return 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002890}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002891
2892/*****************************************************************************
2893 *
2894 * The following functions belong to the exported interface of AMD IOMMU
2895 *
2896 * This interface allows access to lower level functions of the IOMMU
2897 * like protection domain handling and assignement of devices to domains
2898 * which is not possible with the dma_ops interface.
2899 *
2900 *****************************************************************************/
2901
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002902static void cleanup_domain(struct protection_domain *domain)
2903{
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002904 struct iommu_dev_data *entry;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002905 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002906
2907 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2908
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002909 while (!list_empty(&domain->dev_list)) {
2910 entry = list_first_entry(&domain->dev_list,
2911 struct iommu_dev_data, list);
2912 __detach_device(entry);
Joerg Roedel492667d2009-11-27 13:25:47 +01002913 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002914
2915 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2916}
2917
Joerg Roedel26508152009-08-26 16:52:40 +02002918static void protection_domain_free(struct protection_domain *domain)
2919{
2920 if (!domain)
2921 return;
2922
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002923 del_domain_from_list(domain);
2924
Joerg Roedel26508152009-08-26 16:52:40 +02002925 if (domain->id)
2926 domain_id_free(domain->id);
2927
2928 kfree(domain);
2929}
2930
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002931static int protection_domain_init(struct protection_domain *domain)
2932{
2933 spin_lock_init(&domain->lock);
2934 mutex_init(&domain->api_lock);
2935 domain->id = domain_id_alloc();
2936 if (!domain->id)
2937 return -ENOMEM;
2938 INIT_LIST_HEAD(&domain->dev_list);
2939
2940 return 0;
2941}
2942
Joerg Roedel26508152009-08-26 16:52:40 +02002943static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002944{
2945 struct protection_domain *domain;
2946
2947 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2948 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002949 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002950
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002951 if (protection_domain_init(domain))
Joerg Roedel26508152009-08-26 16:52:40 +02002952 goto out_err;
2953
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002954 add_domain_to_list(domain);
2955
Joerg Roedel26508152009-08-26 16:52:40 +02002956 return domain;
2957
2958out_err:
2959 kfree(domain);
2960
2961 return NULL;
2962}
2963
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002964static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2965{
2966 struct protection_domain *pdomain;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002967 struct dma_ops_domain *dma_domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002968
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002969 switch (type) {
2970 case IOMMU_DOMAIN_UNMANAGED:
2971 pdomain = protection_domain_alloc();
2972 if (!pdomain)
2973 return NULL;
2974
2975 pdomain->mode = PAGE_MODE_3_LEVEL;
2976 pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2977 if (!pdomain->pt_root) {
2978 protection_domain_free(pdomain);
2979 return NULL;
2980 }
2981
2982 pdomain->domain.geometry.aperture_start = 0;
2983 pdomain->domain.geometry.aperture_end = ~0ULL;
2984 pdomain->domain.geometry.force_aperture = true;
2985
2986 break;
2987 case IOMMU_DOMAIN_DMA:
2988 dma_domain = dma_ops_domain_alloc();
2989 if (!dma_domain) {
2990 pr_err("AMD-Vi: Failed to allocate\n");
2991 return NULL;
2992 }
2993 pdomain = &dma_domain->domain;
2994 break;
Joerg Roedel07f643a2015-05-28 18:41:41 +02002995 case IOMMU_DOMAIN_IDENTITY:
2996 pdomain = protection_domain_alloc();
2997 if (!pdomain)
2998 return NULL;
2999
3000 pdomain->mode = PAGE_MODE_NONE;
3001 break;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02003002 default:
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003003 return NULL;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02003004 }
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003005
3006 return &pdomain->domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003007}
3008
3009static void amd_iommu_domain_free(struct iommu_domain *dom)
Joerg Roedel26508152009-08-26 16:52:40 +02003010{
3011 struct protection_domain *domain;
3012
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003013 if (!dom)
Joerg Roedel98383fc2008-12-02 18:34:12 +01003014 return;
3015
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003016 domain = to_pdomain(dom);
3017
Joerg Roedel98383fc2008-12-02 18:34:12 +01003018 if (domain->dev_cnt > 0)
3019 cleanup_domain(domain);
3020
3021 BUG_ON(domain->dev_cnt != 0);
3022
Joerg Roedel132bd682011-11-17 14:18:46 +01003023 if (domain->mode != PAGE_MODE_NONE)
3024 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003025
Joerg Roedel52815b72011-11-17 17:24:28 +01003026 if (domain->flags & PD_IOMMUV2_MASK)
3027 free_gcr3_table(domain);
3028
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003029 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003030}
3031
Joerg Roedel684f2882008-12-08 12:07:44 +01003032static void amd_iommu_detach_device(struct iommu_domain *dom,
3033 struct device *dev)
3034{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003035 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003036 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003037 u16 devid;
3038
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003039 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003040 return;
3041
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003042 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003043
Joerg Roedel657cbb62009-11-23 15:26:46 +01003044 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003045 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003046
3047 iommu = amd_iommu_rlookup_table[devid];
3048 if (!iommu)
3049 return;
3050
Joerg Roedel684f2882008-12-08 12:07:44 +01003051 iommu_completion_wait(iommu);
3052}
3053
Joerg Roedel01106062008-12-02 19:34:11 +01003054static int amd_iommu_attach_device(struct iommu_domain *dom,
3055 struct device *dev)
3056{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003057 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel657cbb62009-11-23 15:26:46 +01003058 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003059 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003060 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003061
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003062 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003063 return -EINVAL;
3064
Joerg Roedel657cbb62009-11-23 15:26:46 +01003065 dev_data = dev->archdata.iommu;
3066
Joerg Roedelf62dda62011-06-09 12:55:35 +02003067 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003068 if (!iommu)
3069 return -EINVAL;
3070
Joerg Roedel657cbb62009-11-23 15:26:46 +01003071 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003072 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003073
Joerg Roedel15898bb2009-11-24 15:39:42 +01003074 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003075
3076 iommu_completion_wait(iommu);
3077
Joerg Roedel15898bb2009-11-24 15:39:42 +01003078 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003079}
3080
Joerg Roedel468e2362010-01-21 16:37:36 +01003081static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003082 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003083{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003084 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003085 int prot = 0;
3086 int ret;
3087
Joerg Roedel132bd682011-11-17 14:18:46 +01003088 if (domain->mode == PAGE_MODE_NONE)
3089 return -EINVAL;
3090
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003091 if (iommu_prot & IOMMU_READ)
3092 prot |= IOMMU_PROT_IR;
3093 if (iommu_prot & IOMMU_WRITE)
3094 prot |= IOMMU_PROT_IW;
3095
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003096 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003097 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003098 mutex_unlock(&domain->api_lock);
3099
Joerg Roedel795e74f72010-05-11 17:40:57 +02003100 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003101}
3102
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003103static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3104 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003105{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003106 struct protection_domain *domain = to_pdomain(dom);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003107 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003108
Joerg Roedel132bd682011-11-17 14:18:46 +01003109 if (domain->mode == PAGE_MODE_NONE)
3110 return -EINVAL;
3111
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003112 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003113 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003114 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003115
Joerg Roedel17b124b2011-04-06 18:01:35 +02003116 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003117
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003118 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003119}
3120
Joerg Roedel645c4c82008-12-02 20:05:50 +01003121static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547ac2013-03-29 01:23:58 +05303122 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01003123{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003124 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel3039ca12015-04-01 14:58:48 +02003125 unsigned long offset_mask, pte_pgsize;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003126 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003127
Joerg Roedel132bd682011-11-17 14:18:46 +01003128 if (domain->mode == PAGE_MODE_NONE)
3129 return iova;
3130
Joerg Roedel3039ca12015-04-01 14:58:48 +02003131 pte = fetch_pte(domain, iova, &pte_pgsize);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003132
Joerg Roedela6d41a42009-09-02 17:08:55 +02003133 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003134 return 0;
3135
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003136 offset_mask = pte_pgsize - 1;
3137 __pte = *pte & PM_ADDR_MASK;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003138
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003139 return (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003140}
3141
Joerg Roedelab636482014-09-05 10:48:21 +02003142static bool amd_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003143{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003144 switch (cap) {
3145 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedelab636482014-09-05 10:48:21 +02003146 return true;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003147 case IOMMU_CAP_INTR_REMAP:
Joerg Roedelab636482014-09-05 10:48:21 +02003148 return (irq_remapping_enabled == 1);
Will Deaconcfdeec22014-10-27 11:24:48 +00003149 case IOMMU_CAP_NOEXEC:
3150 return false;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003151 }
3152
Joerg Roedelab636482014-09-05 10:48:21 +02003153 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003154}
3155
Joerg Roedel35cf2482015-05-28 18:41:37 +02003156static void amd_iommu_get_dm_regions(struct device *dev,
3157 struct list_head *head)
3158{
3159 struct unity_map_entry *entry;
3160 u16 devid;
3161
3162 devid = get_device_id(dev);
3163
3164 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
3165 struct iommu_dm_region *region;
3166
3167 if (devid < entry->devid_start || devid > entry->devid_end)
3168 continue;
3169
3170 region = kzalloc(sizeof(*region), GFP_KERNEL);
3171 if (!region) {
3172 pr_err("Out of memory allocating dm-regions for %s\n",
3173 dev_name(dev));
3174 return;
3175 }
3176
3177 region->start = entry->address_start;
3178 region->length = entry->address_end - entry->address_start;
3179 if (entry->prot & IOMMU_PROT_IR)
3180 region->prot |= IOMMU_READ;
3181 if (entry->prot & IOMMU_PROT_IW)
3182 region->prot |= IOMMU_WRITE;
3183
3184 list_add_tail(&region->list, head);
3185 }
3186}
3187
3188static void amd_iommu_put_dm_regions(struct device *dev,
3189 struct list_head *head)
3190{
3191 struct iommu_dm_region *entry, *next;
3192
3193 list_for_each_entry_safe(entry, next, head, list)
3194 kfree(entry);
3195}
3196
Thierry Redingb22f6432014-06-27 09:03:12 +02003197static const struct iommu_ops amd_iommu_ops = {
Joerg Roedelab636482014-09-05 10:48:21 +02003198 .capable = amd_iommu_capable,
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003199 .domain_alloc = amd_iommu_domain_alloc,
3200 .domain_free = amd_iommu_domain_free,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003201 .attach_dev = amd_iommu_attach_device,
3202 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003203 .map = amd_iommu_map,
3204 .unmap = amd_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07003205 .map_sg = default_iommu_map_sg,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003206 .iova_to_phys = amd_iommu_iova_to_phys,
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02003207 .add_device = amd_iommu_add_device,
3208 .remove_device = amd_iommu_remove_device,
Joerg Roedela960fad2015-10-21 23:51:39 +02003209 .device_group = pci_device_group,
Joerg Roedel35cf2482015-05-28 18:41:37 +02003210 .get_dm_regions = amd_iommu_get_dm_regions,
3211 .put_dm_regions = amd_iommu_put_dm_regions,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003212 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003213};
3214
Joerg Roedel0feae532009-08-26 15:26:30 +02003215/*****************************************************************************
3216 *
3217 * The next functions do a basic initialization of IOMMU for pass through
3218 * mode
3219 *
3220 * In passthrough mode the IOMMU is initialized and enabled but not used for
3221 * DMA-API translation.
3222 *
3223 *****************************************************************************/
3224
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003225/* IOMMUv2 specific functions */
3226int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3227{
3228 return atomic_notifier_chain_register(&ppr_notifier, nb);
3229}
3230EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3231
3232int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3233{
3234 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3235}
3236EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003237
3238void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3239{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003240 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel132bd682011-11-17 14:18:46 +01003241 unsigned long flags;
3242
3243 spin_lock_irqsave(&domain->lock, flags);
3244
3245 /* Update data structure */
3246 domain->mode = PAGE_MODE_NONE;
3247 domain->updated = true;
3248
3249 /* Make changes visible to IOMMUs */
3250 update_domain(domain);
3251
3252 /* Page-table is not visible to IOMMU anymore, so free it */
3253 free_pagetable(domain);
3254
3255 spin_unlock_irqrestore(&domain->lock, flags);
3256}
3257EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003258
3259int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3260{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003261 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel52815b72011-11-17 17:24:28 +01003262 unsigned long flags;
3263 int levels, ret;
3264
3265 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3266 return -EINVAL;
3267
3268 /* Number of GCR3 table levels required */
3269 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3270 levels += 1;
3271
3272 if (levels > amd_iommu_max_glx_val)
3273 return -EINVAL;
3274
3275 spin_lock_irqsave(&domain->lock, flags);
3276
3277 /*
3278 * Save us all sanity checks whether devices already in the
3279 * domain support IOMMUv2. Just force that the domain has no
3280 * devices attached when it is switched into IOMMUv2 mode.
3281 */
3282 ret = -EBUSY;
3283 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3284 goto out;
3285
3286 ret = -ENOMEM;
3287 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3288 if (domain->gcr3_tbl == NULL)
3289 goto out;
3290
3291 domain->glx = levels;
3292 domain->flags |= PD_IOMMUV2_MASK;
3293 domain->updated = true;
3294
3295 update_domain(domain);
3296
3297 ret = 0;
3298
3299out:
3300 spin_unlock_irqrestore(&domain->lock, flags);
3301
3302 return ret;
3303}
3304EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003305
3306static int __flush_pasid(struct protection_domain *domain, int pasid,
3307 u64 address, bool size)
3308{
3309 struct iommu_dev_data *dev_data;
3310 struct iommu_cmd cmd;
3311 int i, ret;
3312
3313 if (!(domain->flags & PD_IOMMUV2_MASK))
3314 return -EINVAL;
3315
3316 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3317
3318 /*
3319 * IOMMU TLB needs to be flushed before Device TLB to
3320 * prevent device TLB refill from IOMMU TLB
3321 */
3322 for (i = 0; i < amd_iommus_present; ++i) {
3323 if (domain->dev_iommu[i] == 0)
3324 continue;
3325
3326 ret = iommu_queue_command(amd_iommus[i], &cmd);
3327 if (ret != 0)
3328 goto out;
3329 }
3330
3331 /* Wait until IOMMU TLB flushes are complete */
3332 domain_flush_complete(domain);
3333
3334 /* Now flush device TLBs */
3335 list_for_each_entry(dev_data, &domain->dev_list, list) {
3336 struct amd_iommu *iommu;
3337 int qdep;
3338
Joerg Roedel1c1cc452015-07-30 11:24:45 +02003339 /*
3340 There might be non-IOMMUv2 capable devices in an IOMMUv2
3341 * domain.
3342 */
3343 if (!dev_data->ats.enabled)
3344 continue;
Joerg Roedel22e266c2011-11-21 15:59:08 +01003345
3346 qdep = dev_data->ats.qdep;
3347 iommu = amd_iommu_rlookup_table[dev_data->devid];
3348
3349 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3350 qdep, address, size);
3351
3352 ret = iommu_queue_command(iommu, &cmd);
3353 if (ret != 0)
3354 goto out;
3355 }
3356
3357 /* Wait until all device TLBs are flushed */
3358 domain_flush_complete(domain);
3359
3360 ret = 0;
3361
3362out:
3363
3364 return ret;
3365}
3366
3367static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3368 u64 address)
3369{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003370 INC_STATS_COUNTER(invalidate_iotlb);
3371
Joerg Roedel22e266c2011-11-21 15:59:08 +01003372 return __flush_pasid(domain, pasid, address, false);
3373}
3374
3375int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3376 u64 address)
3377{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003378 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003379 unsigned long flags;
3380 int ret;
3381
3382 spin_lock_irqsave(&domain->lock, flags);
3383 ret = __amd_iommu_flush_page(domain, pasid, address);
3384 spin_unlock_irqrestore(&domain->lock, flags);
3385
3386 return ret;
3387}
3388EXPORT_SYMBOL(amd_iommu_flush_page);
3389
3390static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3391{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003392 INC_STATS_COUNTER(invalidate_iotlb_all);
3393
Joerg Roedel22e266c2011-11-21 15:59:08 +01003394 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3395 true);
3396}
3397
3398int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3399{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003400 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003401 unsigned long flags;
3402 int ret;
3403
3404 spin_lock_irqsave(&domain->lock, flags);
3405 ret = __amd_iommu_flush_tlb(domain, pasid);
3406 spin_unlock_irqrestore(&domain->lock, flags);
3407
3408 return ret;
3409}
3410EXPORT_SYMBOL(amd_iommu_flush_tlb);
3411
Joerg Roedelb16137b2011-11-21 16:50:23 +01003412static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3413{
3414 int index;
3415 u64 *pte;
3416
3417 while (true) {
3418
3419 index = (pasid >> (9 * level)) & 0x1ff;
3420 pte = &root[index];
3421
3422 if (level == 0)
3423 break;
3424
3425 if (!(*pte & GCR3_VALID)) {
3426 if (!alloc)
3427 return NULL;
3428
3429 root = (void *)get_zeroed_page(GFP_ATOMIC);
3430 if (root == NULL)
3431 return NULL;
3432
3433 *pte = __pa(root) | GCR3_VALID;
3434 }
3435
3436 root = __va(*pte & PAGE_MASK);
3437
3438 level -= 1;
3439 }
3440
3441 return pte;
3442}
3443
3444static int __set_gcr3(struct protection_domain *domain, int pasid,
3445 unsigned long cr3)
3446{
3447 u64 *pte;
3448
3449 if (domain->mode != PAGE_MODE_NONE)
3450 return -EINVAL;
3451
3452 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3453 if (pte == NULL)
3454 return -ENOMEM;
3455
3456 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3457
3458 return __amd_iommu_flush_tlb(domain, pasid);
3459}
3460
3461static int __clear_gcr3(struct protection_domain *domain, int pasid)
3462{
3463 u64 *pte;
3464
3465 if (domain->mode != PAGE_MODE_NONE)
3466 return -EINVAL;
3467
3468 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3469 if (pte == NULL)
3470 return 0;
3471
3472 *pte = 0;
3473
3474 return __amd_iommu_flush_tlb(domain, pasid);
3475}
3476
3477int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3478 unsigned long cr3)
3479{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003480 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003481 unsigned long flags;
3482 int ret;
3483
3484 spin_lock_irqsave(&domain->lock, flags);
3485 ret = __set_gcr3(domain, pasid, cr3);
3486 spin_unlock_irqrestore(&domain->lock, flags);
3487
3488 return ret;
3489}
3490EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3491
3492int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3493{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003494 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003495 unsigned long flags;
3496 int ret;
3497
3498 spin_lock_irqsave(&domain->lock, flags);
3499 ret = __clear_gcr3(domain, pasid);
3500 spin_unlock_irqrestore(&domain->lock, flags);
3501
3502 return ret;
3503}
3504EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003505
3506int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3507 int status, int tag)
3508{
3509 struct iommu_dev_data *dev_data;
3510 struct amd_iommu *iommu;
3511 struct iommu_cmd cmd;
3512
Joerg Roedel399be2f2011-12-01 16:53:47 +01003513 INC_STATS_COUNTER(complete_ppr);
3514
Joerg Roedelc99afa22011-11-21 18:19:25 +01003515 dev_data = get_dev_data(&pdev->dev);
3516 iommu = amd_iommu_rlookup_table[dev_data->devid];
3517
3518 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3519 tag, dev_data->pri_tlp);
3520
3521 return iommu_queue_command(iommu, &cmd);
3522}
3523EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003524
3525struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3526{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003527 struct protection_domain *pdomain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003528
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003529 pdomain = get_domain(&pdev->dev);
3530 if (IS_ERR(pdomain))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003531 return NULL;
3532
3533 /* Only return IOMMUv2 domains */
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003534 if (!(pdomain->flags & PD_IOMMUV2_MASK))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003535 return NULL;
3536
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003537 return &pdomain->domain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003538}
3539EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003540
3541void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3542{
3543 struct iommu_dev_data *dev_data;
3544
3545 if (!amd_iommu_v2_supported())
3546 return;
3547
3548 dev_data = get_dev_data(&pdev->dev);
3549 dev_data->errata |= (1 << erratum);
3550}
3551EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003552
3553int amd_iommu_device_info(struct pci_dev *pdev,
3554 struct amd_iommu_device_info *info)
3555{
3556 int max_pasids;
3557 int pos;
3558
3559 if (pdev == NULL || info == NULL)
3560 return -EINVAL;
3561
3562 if (!amd_iommu_v2_supported())
3563 return -EINVAL;
3564
3565 memset(info, 0, sizeof(*info));
3566
3567 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3568 if (pos)
3569 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3570
3571 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3572 if (pos)
3573 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3574
3575 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3576 if (pos) {
3577 int features;
3578
3579 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3580 max_pasids = min(max_pasids, (1 << 20));
3581
3582 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3583 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3584
3585 features = pci_pasid_features(pdev);
3586 if (features & PCI_PASID_CAP_EXEC)
3587 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3588 if (features & PCI_PASID_CAP_PRIV)
3589 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3590 }
3591
3592 return 0;
3593}
3594EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003595
3596#ifdef CONFIG_IRQ_REMAP
3597
3598/*****************************************************************************
3599 *
3600 * Interrupt Remapping Implementation
3601 *
3602 *****************************************************************************/
3603
3604union irte {
3605 u32 val;
3606 struct {
3607 u32 valid : 1,
3608 no_fault : 1,
3609 int_type : 3,
3610 rq_eoi : 1,
3611 dm : 1,
3612 rsvd_1 : 1,
3613 destination : 8,
3614 vector : 8,
3615 rsvd_2 : 8;
3616 } fields;
3617};
3618
Jiang Liu9c724962015-04-14 10:29:52 +08003619struct irq_2_irte {
3620 u16 devid; /* Device ID for IRTE table */
3621 u16 index; /* Index into IRTE table*/
3622};
3623
Jiang Liu7c71d302015-04-13 14:11:33 +08003624struct amd_ir_data {
3625 struct irq_2_irte irq_2_irte;
3626 union irte irte_entry;
3627 union {
3628 struct msi_msg msi_entry;
3629 };
3630};
3631
3632static struct irq_chip amd_ir_chip;
3633
Joerg Roedel2b324502012-06-21 16:29:10 +02003634#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3635#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3636#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3637#define DTE_IRQ_REMAP_ENABLE 1ULL
3638
3639static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3640{
3641 u64 dte;
3642
3643 dte = amd_iommu_dev_table[devid].data[2];
3644 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3645 dte |= virt_to_phys(table->table);
3646 dte |= DTE_IRQ_REMAP_INTCTL;
3647 dte |= DTE_IRQ_TABLE_LEN;
3648 dte |= DTE_IRQ_REMAP_ENABLE;
3649
3650 amd_iommu_dev_table[devid].data[2] = dte;
3651}
3652
3653#define IRTE_ALLOCATED (~1U)
3654
3655static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3656{
3657 struct irq_remap_table *table = NULL;
3658 struct amd_iommu *iommu;
3659 unsigned long flags;
3660 u16 alias;
3661
3662 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3663
3664 iommu = amd_iommu_rlookup_table[devid];
3665 if (!iommu)
3666 goto out_unlock;
3667
3668 table = irq_lookup_table[devid];
3669 if (table)
3670 goto out;
3671
3672 alias = amd_iommu_alias_table[devid];
3673 table = irq_lookup_table[alias];
3674 if (table) {
3675 irq_lookup_table[devid] = table;
3676 set_dte_irq_entry(devid, table);
3677 iommu_flush_dte(iommu, devid);
3678 goto out;
3679 }
3680
3681 /* Nothing there yet, allocate new irq remapping table */
3682 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3683 if (!table)
3684 goto out;
3685
Joerg Roedel197887f2013-04-09 21:14:08 +02003686 /* Initialize table spin-lock */
3687 spin_lock_init(&table->lock);
3688
Joerg Roedel2b324502012-06-21 16:29:10 +02003689 if (ioapic)
3690 /* Keep the first 32 indexes free for IOAPIC interrupts */
3691 table->min_index = 32;
3692
3693 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3694 if (!table->table) {
3695 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003696 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003697 goto out;
3698 }
3699
3700 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3701
3702 if (ioapic) {
3703 int i;
3704
3705 for (i = 0; i < 32; ++i)
3706 table->table[i] = IRTE_ALLOCATED;
3707 }
3708
3709 irq_lookup_table[devid] = table;
3710 set_dte_irq_entry(devid, table);
3711 iommu_flush_dte(iommu, devid);
3712 if (devid != alias) {
3713 irq_lookup_table[alias] = table;
Alex Williamsone028a9e2014-04-22 10:08:40 -06003714 set_dte_irq_entry(alias, table);
Joerg Roedel2b324502012-06-21 16:29:10 +02003715 iommu_flush_dte(iommu, alias);
3716 }
3717
3718out:
3719 iommu_completion_wait(iommu);
3720
3721out_unlock:
3722 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3723
3724 return table;
3725}
3726
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003727static int alloc_irq_index(u16 devid, int count)
Joerg Roedel2b324502012-06-21 16:29:10 +02003728{
3729 struct irq_remap_table *table;
3730 unsigned long flags;
3731 int index, c;
3732
3733 table = get_irq_table(devid, false);
3734 if (!table)
3735 return -ENODEV;
3736
3737 spin_lock_irqsave(&table->lock, flags);
3738
3739 /* Scan table for free entries */
3740 for (c = 0, index = table->min_index;
3741 index < MAX_IRQS_PER_TABLE;
3742 ++index) {
3743 if (table->table[index] == 0)
3744 c += 1;
3745 else
3746 c = 0;
3747
3748 if (c == count) {
Joerg Roedel2b324502012-06-21 16:29:10 +02003749 for (; c != 0; --c)
3750 table->table[index - c + 1] = IRTE_ALLOCATED;
3751
3752 index -= count - 1;
Joerg Roedel2b324502012-06-21 16:29:10 +02003753 goto out;
3754 }
3755 }
3756
3757 index = -ENOSPC;
3758
3759out:
3760 spin_unlock_irqrestore(&table->lock, flags);
3761
3762 return index;
3763}
3764
Joerg Roedel2b324502012-06-21 16:29:10 +02003765static int modify_irte(u16 devid, int index, union irte irte)
3766{
3767 struct irq_remap_table *table;
3768 struct amd_iommu *iommu;
3769 unsigned long flags;
3770
3771 iommu = amd_iommu_rlookup_table[devid];
3772 if (iommu == NULL)
3773 return -EINVAL;
3774
3775 table = get_irq_table(devid, false);
3776 if (!table)
3777 return -ENOMEM;
3778
3779 spin_lock_irqsave(&table->lock, flags);
3780 table->table[index] = irte.val;
3781 spin_unlock_irqrestore(&table->lock, flags);
3782
3783 iommu_flush_irt(iommu, devid);
3784 iommu_completion_wait(iommu);
3785
3786 return 0;
3787}
3788
3789static void free_irte(u16 devid, int index)
3790{
3791 struct irq_remap_table *table;
3792 struct amd_iommu *iommu;
3793 unsigned long flags;
3794
3795 iommu = amd_iommu_rlookup_table[devid];
3796 if (iommu == NULL)
3797 return;
3798
3799 table = get_irq_table(devid, false);
3800 if (!table)
3801 return;
3802
3803 spin_lock_irqsave(&table->lock, flags);
3804 table->table[index] = 0;
3805 spin_unlock_irqrestore(&table->lock, flags);
3806
3807 iommu_flush_irt(iommu, devid);
3808 iommu_completion_wait(iommu);
3809}
3810
Jiang Liu7c71d302015-04-13 14:11:33 +08003811static int get_devid(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003812{
Jiang Liu7c71d302015-04-13 14:11:33 +08003813 int devid = -1;
Joerg Roedel5527de72012-06-26 11:17:32 +02003814
Jiang Liu7c71d302015-04-13 14:11:33 +08003815 switch (info->type) {
3816 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3817 devid = get_ioapic_devid(info->ioapic_id);
3818 break;
3819 case X86_IRQ_ALLOC_TYPE_HPET:
3820 devid = get_hpet_devid(info->hpet_id);
3821 break;
3822 case X86_IRQ_ALLOC_TYPE_MSI:
3823 case X86_IRQ_ALLOC_TYPE_MSIX:
3824 devid = get_device_id(&info->msi_dev->dev);
3825 break;
3826 default:
3827 BUG_ON(1);
3828 break;
Joerg Roedel5527de72012-06-26 11:17:32 +02003829 }
3830
Jiang Liu7c71d302015-04-13 14:11:33 +08003831 return devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003832}
3833
Jiang Liu7c71d302015-04-13 14:11:33 +08003834static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003835{
Jiang Liu7c71d302015-04-13 14:11:33 +08003836 struct amd_iommu *iommu;
3837 int devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003838
Jiang Liu7c71d302015-04-13 14:11:33 +08003839 if (!info)
3840 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003841
Jiang Liu7c71d302015-04-13 14:11:33 +08003842 devid = get_devid(info);
3843 if (devid >= 0) {
3844 iommu = amd_iommu_rlookup_table[devid];
3845 if (iommu)
3846 return iommu->ir_domain;
3847 }
Joerg Roedel5527de72012-06-26 11:17:32 +02003848
Jiang Liu7c71d302015-04-13 14:11:33 +08003849 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003850}
3851
Jiang Liu7c71d302015-04-13 14:11:33 +08003852static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003853{
Jiang Liu7c71d302015-04-13 14:11:33 +08003854 struct amd_iommu *iommu;
3855 int devid;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003856
Jiang Liu7c71d302015-04-13 14:11:33 +08003857 if (!info)
3858 return NULL;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003859
Jiang Liu7c71d302015-04-13 14:11:33 +08003860 switch (info->type) {
3861 case X86_IRQ_ALLOC_TYPE_MSI:
3862 case X86_IRQ_ALLOC_TYPE_MSIX:
3863 devid = get_device_id(&info->msi_dev->dev);
Dan Carpenter1fb260b2016-01-07 12:36:06 +03003864 iommu = amd_iommu_rlookup_table[devid];
3865 if (iommu)
3866 return iommu->msi_domain;
Jiang Liu7c71d302015-04-13 14:11:33 +08003867 break;
3868 default:
3869 break;
3870 }
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003871
Jiang Liu7c71d302015-04-13 14:11:33 +08003872 return NULL;
Joerg Roedeld9761952012-06-26 16:00:08 +02003873}
3874
Joerg Roedel6b474b82012-06-26 16:46:04 +02003875struct irq_remap_ops amd_iommu_irq_ops = {
Joerg Roedel6b474b82012-06-26 16:46:04 +02003876 .prepare = amd_iommu_prepare,
3877 .enable = amd_iommu_enable,
3878 .disable = amd_iommu_disable,
3879 .reenable = amd_iommu_reenable,
3880 .enable_faulting = amd_iommu_enable_faulting,
Jiang Liu7c71d302015-04-13 14:11:33 +08003881 .get_ir_irq_domain = get_ir_irq_domain,
3882 .get_irq_domain = get_irq_domain,
Joerg Roedel6b474b82012-06-26 16:46:04 +02003883};
Jiang Liu7c71d302015-04-13 14:11:33 +08003884
3885static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3886 struct irq_cfg *irq_cfg,
3887 struct irq_alloc_info *info,
3888 int devid, int index, int sub_handle)
3889{
3890 struct irq_2_irte *irte_info = &data->irq_2_irte;
3891 struct msi_msg *msg = &data->msi_entry;
3892 union irte *irte = &data->irte_entry;
3893 struct IO_APIC_route_entry *entry;
3894
Jiang Liu7c71d302015-04-13 14:11:33 +08003895 data->irq_2_irte.devid = devid;
3896 data->irq_2_irte.index = index + sub_handle;
3897
3898 /* Setup IRTE for IOMMU */
3899 irte->val = 0;
3900 irte->fields.vector = irq_cfg->vector;
3901 irte->fields.int_type = apic->irq_delivery_mode;
3902 irte->fields.destination = irq_cfg->dest_apicid;
3903 irte->fields.dm = apic->irq_dest_mode;
3904 irte->fields.valid = 1;
3905
3906 switch (info->type) {
3907 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3908 /* Setup IOAPIC entry */
3909 entry = info->ioapic_entry;
3910 info->ioapic_entry = NULL;
3911 memset(entry, 0, sizeof(*entry));
3912 entry->vector = index;
3913 entry->mask = 0;
3914 entry->trigger = info->ioapic_trigger;
3915 entry->polarity = info->ioapic_polarity;
3916 /* Mask level triggered irqs. */
3917 if (info->ioapic_trigger)
3918 entry->mask = 1;
3919 break;
3920
3921 case X86_IRQ_ALLOC_TYPE_HPET:
3922 case X86_IRQ_ALLOC_TYPE_MSI:
3923 case X86_IRQ_ALLOC_TYPE_MSIX:
3924 msg->address_hi = MSI_ADDR_BASE_HI;
3925 msg->address_lo = MSI_ADDR_BASE_LO;
3926 msg->data = irte_info->index;
3927 break;
3928
3929 default:
3930 BUG_ON(1);
3931 break;
3932 }
3933}
3934
3935static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
3936 unsigned int nr_irqs, void *arg)
3937{
3938 struct irq_alloc_info *info = arg;
3939 struct irq_data *irq_data;
3940 struct amd_ir_data *data;
3941 struct irq_cfg *cfg;
3942 int i, ret, devid;
3943 int index = -1;
3944
3945 if (!info)
3946 return -EINVAL;
3947 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
3948 info->type != X86_IRQ_ALLOC_TYPE_MSIX)
3949 return -EINVAL;
3950
3951 /*
3952 * With IRQ remapping enabled, don't need contiguous CPU vectors
3953 * to support multiple MSI interrupts.
3954 */
3955 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
3956 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
3957
3958 devid = get_devid(info);
3959 if (devid < 0)
3960 return -EINVAL;
3961
3962 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
3963 if (ret < 0)
3964 return ret;
3965
Jiang Liu7c71d302015-04-13 14:11:33 +08003966 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
3967 if (get_irq_table(devid, true))
3968 index = info->ioapic_pin;
3969 else
3970 ret = -ENOMEM;
3971 } else {
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003972 index = alloc_irq_index(devid, nr_irqs);
Jiang Liu7c71d302015-04-13 14:11:33 +08003973 }
3974 if (index < 0) {
3975 pr_warn("Failed to allocate IRTE\n");
Jiang Liu7c71d302015-04-13 14:11:33 +08003976 goto out_free_parent;
3977 }
3978
3979 for (i = 0; i < nr_irqs; i++) {
3980 irq_data = irq_domain_get_irq_data(domain, virq + i);
3981 cfg = irqd_cfg(irq_data);
3982 if (!irq_data || !cfg) {
3983 ret = -EINVAL;
3984 goto out_free_data;
3985 }
3986
Joerg Roedela130e692015-08-13 11:07:25 +02003987 ret = -ENOMEM;
3988 data = kzalloc(sizeof(*data), GFP_KERNEL);
3989 if (!data)
3990 goto out_free_data;
3991
Jiang Liu7c71d302015-04-13 14:11:33 +08003992 irq_data->hwirq = (devid << 16) + i;
3993 irq_data->chip_data = data;
3994 irq_data->chip = &amd_ir_chip;
3995 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
3996 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
3997 }
Joerg Roedela130e692015-08-13 11:07:25 +02003998
Jiang Liu7c71d302015-04-13 14:11:33 +08003999 return 0;
4000
4001out_free_data:
4002 for (i--; i >= 0; i--) {
4003 irq_data = irq_domain_get_irq_data(domain, virq + i);
4004 if (irq_data)
4005 kfree(irq_data->chip_data);
4006 }
4007 for (i = 0; i < nr_irqs; i++)
4008 free_irte(devid, index + i);
4009out_free_parent:
4010 irq_domain_free_irqs_common(domain, virq, nr_irqs);
4011 return ret;
4012}
4013
4014static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
4015 unsigned int nr_irqs)
4016{
4017 struct irq_2_irte *irte_info;
4018 struct irq_data *irq_data;
4019 struct amd_ir_data *data;
4020 int i;
4021
4022 for (i = 0; i < nr_irqs; i++) {
4023 irq_data = irq_domain_get_irq_data(domain, virq + i);
4024 if (irq_data && irq_data->chip_data) {
4025 data = irq_data->chip_data;
4026 irte_info = &data->irq_2_irte;
4027 free_irte(irte_info->devid, irte_info->index);
4028 kfree(data);
4029 }
4030 }
4031 irq_domain_free_irqs_common(domain, virq, nr_irqs);
4032}
4033
4034static void irq_remapping_activate(struct irq_domain *domain,
4035 struct irq_data *irq_data)
4036{
4037 struct amd_ir_data *data = irq_data->chip_data;
4038 struct irq_2_irte *irte_info = &data->irq_2_irte;
4039
4040 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
4041}
4042
4043static void irq_remapping_deactivate(struct irq_domain *domain,
4044 struct irq_data *irq_data)
4045{
4046 struct amd_ir_data *data = irq_data->chip_data;
4047 struct irq_2_irte *irte_info = &data->irq_2_irte;
4048 union irte entry;
4049
4050 entry.val = 0;
4051 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
4052}
4053
4054static struct irq_domain_ops amd_ir_domain_ops = {
4055 .alloc = irq_remapping_alloc,
4056 .free = irq_remapping_free,
4057 .activate = irq_remapping_activate,
4058 .deactivate = irq_remapping_deactivate,
4059};
4060
4061static int amd_ir_set_affinity(struct irq_data *data,
4062 const struct cpumask *mask, bool force)
4063{
4064 struct amd_ir_data *ir_data = data->chip_data;
4065 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
4066 struct irq_cfg *cfg = irqd_cfg(data);
4067 struct irq_data *parent = data->parent_data;
4068 int ret;
4069
4070 ret = parent->chip->irq_set_affinity(parent, mask, force);
4071 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
4072 return ret;
4073
4074 /*
4075 * Atomically updates the IRTE with the new destination, vector
4076 * and flushes the interrupt entry cache.
4077 */
4078 ir_data->irte_entry.fields.vector = cfg->vector;
4079 ir_data->irte_entry.fields.destination = cfg->dest_apicid;
4080 modify_irte(irte_info->devid, irte_info->index, ir_data->irte_entry);
4081
4082 /*
4083 * After this point, all the interrupts will start arriving
4084 * at the new destination. So, time to cleanup the previous
4085 * vector allocation.
4086 */
Jiang Liuc6c20022015-04-14 10:30:02 +08004087 send_cleanup_vector(cfg);
Jiang Liu7c71d302015-04-13 14:11:33 +08004088
4089 return IRQ_SET_MASK_OK_DONE;
4090}
4091
4092static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
4093{
4094 struct amd_ir_data *ir_data = irq_data->chip_data;
4095
4096 *msg = ir_data->msi_entry;
4097}
4098
4099static struct irq_chip amd_ir_chip = {
4100 .irq_ack = ir_ack_apic_edge,
4101 .irq_set_affinity = amd_ir_set_affinity,
4102 .irq_compose_msi_msg = ir_compose_msi_msg,
4103};
4104
4105int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
4106{
4107 iommu->ir_domain = irq_domain_add_tree(NULL, &amd_ir_domain_ops, iommu);
4108 if (!iommu->ir_domain)
4109 return -ENOMEM;
4110
4111 iommu->ir_domain->parent = arch_get_ir_parent_domain();
4112 iommu->msi_domain = arch_create_msi_irq_domain(iommu->ir_domain);
4113
4114 return 0;
4115}
Joerg Roedel2b324502012-06-21 16:29:10 +02004116#endif