blob: 0df651a379df64705749dea205f93ae3751ea010 [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>
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -040022#include <linux/acpi.h>
Joerg Roedelcb41ed82011-04-05 11:00:53 +020023#include <linux/pci-ats.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080024#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010026#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020027#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090028#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020029#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010030#include <linux/iommu.h>
Joerg Roedel815b33f2011-04-06 17:26:49 +020031#include <linux/delay.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020032#include <linux/amd-iommu.h>
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010033#include <linux/notifier.h>
34#include <linux/export.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020035#include <linux/irq.h>
36#include <linux/msi.h>
Joerg Roedel3b839a52015-04-01 14:58:47 +020037#include <linux/dma-contiguous.h>
Jiang Liu7c71d302015-04-13 14:11:33 +080038#include <linux/irqdomain.h>
Joerg Roedel5f6bed52015-12-22 13:34:22 +010039#include <linux/percpu.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020040#include <asm/irq_remapping.h>
41#include <asm/io_apic.h>
42#include <asm/apic.h>
43#include <asm/hw_irq.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020044#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020045#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090046#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010047#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020048#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020049
50#include "amd_iommu_proto.h"
51#include "amd_iommu_types.h"
Joerg Roedel6b474b82012-06-26 16:46:04 +020052#include "irq_remapping.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020053
54#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
55
Joerg Roedel815b33f2011-04-06 17:26:49 +020056#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020057
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020058/*
59 * This bitmap is used to advertise the page sizes our hardware support
60 * to the IOMMU core, which will then use this information to split
61 * physically contiguous memory regions it is mapping into page sizes
62 * that we support.
63 *
Joerg Roedel954e3dd2012-12-02 15:35:37 +010064 * 512GB Pages are not supported due to a hardware bug
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020065 */
Joerg Roedel954e3dd2012-12-02 15:35:37 +010066#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020067
Joerg Roedelb6c02712008-06-26 21:27:53 +020068static DEFINE_RWLOCK(amd_iommu_devtable_lock);
69
Joerg Roedel8fa5f802011-06-09 12:24:45 +020070/* List of all available dev_data structures */
71static LIST_HEAD(dev_data_list);
72static DEFINE_SPINLOCK(dev_data_list_lock);
73
Joerg Roedel6efed632012-06-14 15:52:58 +020074LIST_HEAD(ioapic_map);
75LIST_HEAD(hpet_map);
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -040076LIST_HEAD(acpihid_map);
Joerg Roedel6efed632012-06-14 15:52:58 +020077
Joerg Roedel0feae532009-08-26 15:26:30 +020078/*
79 * Domain for untranslated devices - only allocated
80 * if iommu=pt passed on kernel cmd line.
81 */
Thierry Redingb22f6432014-06-27 09:03:12 +020082static const struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010083
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010084static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010085int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010086
Joerg Roedelac1534a2012-06-21 14:52:40 +020087static struct dma_map_ops amd_iommu_dma_ops;
88
Joerg Roedel431b2a22008-07-11 17:14:22 +020089/*
Joerg Roedel50917e22014-08-05 16:38:38 +020090 * This struct contains device specific data for the IOMMU
91 */
92struct iommu_dev_data {
93 struct list_head list; /* For domain->dev_list */
94 struct list_head dev_data_list; /* For global dev_data_list */
Joerg Roedel50917e22014-08-05 16:38:38 +020095 struct protection_domain *domain; /* Domain the device is bound to */
Joerg Roedel50917e22014-08-05 16:38:38 +020096 u16 devid; /* PCI Device ID */
97 bool iommu_v2; /* Device can make use of IOMMUv2 */
Joerg Roedel1e6a7b02015-07-28 16:58:48 +020098 bool passthrough; /* Device is identity mapped */
Joerg Roedel50917e22014-08-05 16:38:38 +020099 struct {
100 bool enabled;
101 int qdep;
102 } ats; /* ATS state */
103 bool pri_tlp; /* PASID TLB required for
104 PPR completions */
105 u32 errata; /* Bitmap for errata to apply */
106};
107
108/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200109 * general struct to manage commands send to an IOMMU
110 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200111struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +0200112 u32 data[4];
113};
114
Joerg Roedel05152a02012-06-15 16:53:51 +0200115struct kmem_cache *amd_iommu_irq_cache;
116
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200117static void update_domain(struct protection_domain *domain);
Joerg Roedel7a5a5662015-06-30 08:56:11 +0200118static int protection_domain_init(struct protection_domain *domain);
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100119static void detach_device(struct device *dev);
Chris Wrightc1eee672009-05-21 00:56:58 -0700120
Joerg Roedel007b74b2015-12-21 12:53:54 +0100121/*
122 * For dynamic growth the aperture size is split into ranges of 128MB of
123 * DMA address space each. This struct represents one such range.
124 */
125struct aperture_range {
126
Joerg Roedel08c5fb92015-12-21 13:04:49 +0100127 spinlock_t bitmap_lock;
128
Joerg Roedel007b74b2015-12-21 12:53:54 +0100129 /* address allocation bitmap */
130 unsigned long *bitmap;
Joerg Roedelae62d492015-12-21 16:28:45 +0100131 unsigned long offset;
Joerg Roedel60e6a7c2015-12-21 16:53:17 +0100132 unsigned long next_bit;
Joerg Roedel007b74b2015-12-21 12:53:54 +0100133
134 /*
135 * Array of PTE pages for the aperture. In this array we save all the
136 * leaf pages of the domain page table used for the aperture. This way
137 * we don't need to walk the page table to find a specific PTE. We can
138 * just calculate its address in constant time.
139 */
140 u64 *pte_pages[64];
Joerg Roedel007b74b2015-12-21 12:53:54 +0100141};
142
143/*
144 * Data container for a dma_ops specific protection domain
145 */
146struct dma_ops_domain {
147 /* generic protection domain information */
148 struct protection_domain domain;
149
150 /* size of the aperture for the mappings */
151 unsigned long aperture_size;
152
Joerg Roedelebaecb42015-12-21 18:11:32 +0100153 /* aperture index we start searching for free addresses */
Joerg Roedel5f6bed52015-12-22 13:34:22 +0100154 u32 __percpu *next_index;
Joerg Roedel007b74b2015-12-21 12:53:54 +0100155
156 /* address space relevant data */
157 struct aperture_range *aperture[APERTURE_MAX_RANGES];
Joerg Roedel007b74b2015-12-21 12:53:54 +0100158};
159
Joerg Roedel15898bb2009-11-24 15:39:42 +0100160/****************************************************************************
161 *
162 * Helper functions
163 *
164 ****************************************************************************/
165
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100166static struct protection_domain *to_pdomain(struct iommu_domain *dom)
167{
168 return container_of(dom, struct protection_domain, domain);
169}
170
Joerg Roedelf62dda62011-06-09 12:55:35 +0200171static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200172{
173 struct iommu_dev_data *dev_data;
174 unsigned long flags;
175
176 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
177 if (!dev_data)
178 return NULL;
179
Joerg Roedelf62dda62011-06-09 12:55:35 +0200180 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200181
182 spin_lock_irqsave(&dev_data_list_lock, flags);
183 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
184 spin_unlock_irqrestore(&dev_data_list_lock, flags);
185
186 return dev_data;
187}
188
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200189static struct iommu_dev_data *search_dev_data(u16 devid)
190{
191 struct iommu_dev_data *dev_data;
192 unsigned long flags;
193
194 spin_lock_irqsave(&dev_data_list_lock, flags);
195 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
196 if (dev_data->devid == devid)
197 goto out_unlock;
198 }
199
200 dev_data = NULL;
201
202out_unlock:
203 spin_unlock_irqrestore(&dev_data_list_lock, flags);
204
205 return dev_data;
206}
207
208static struct iommu_dev_data *find_dev_data(u16 devid)
209{
210 struct iommu_dev_data *dev_data;
211
212 dev_data = search_dev_data(devid);
213
214 if (dev_data == NULL)
215 dev_data = alloc_dev_data(devid);
216
217 return dev_data;
218}
219
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400220static inline int match_hid_uid(struct device *dev,
221 struct acpihid_map_entry *entry)
222{
223 const char *hid, *uid;
224
225 hid = acpi_device_hid(ACPI_COMPANION(dev));
226 uid = acpi_device_uid(ACPI_COMPANION(dev));
227
228 if (!hid || !(*hid))
229 return -ENODEV;
230
231 if (!uid || !(*uid))
232 return strcmp(hid, entry->hid);
233
234 if (!(*entry->uid))
235 return strcmp(hid, entry->hid);
236
237 return (strcmp(hid, entry->hid) || strcmp(uid, entry->uid));
238}
239
240static inline u16 get_pci_device_id(struct device *dev)
Joerg Roedel15898bb2009-11-24 15:39:42 +0100241{
242 struct pci_dev *pdev = to_pci_dev(dev);
243
Shuah Khan6f2729b2013-02-27 17:07:30 -0700244 return PCI_DEVID(pdev->bus->number, pdev->devfn);
Joerg Roedel15898bb2009-11-24 15:39:42 +0100245}
246
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400247static inline int get_acpihid_device_id(struct device *dev,
248 struct acpihid_map_entry **entry)
249{
250 struct acpihid_map_entry *p;
251
252 list_for_each_entry(p, &acpihid_map, list) {
253 if (!match_hid_uid(dev, p)) {
254 if (entry)
255 *entry = p;
256 return p->devid;
257 }
258 }
259 return -EINVAL;
260}
261
262static inline int get_device_id(struct device *dev)
263{
264 int devid;
265
266 if (dev_is_pci(dev))
267 devid = get_pci_device_id(dev);
268 else
269 devid = get_acpihid_device_id(dev, NULL);
270
271 return devid;
272}
273
Joerg Roedel657cbb62009-11-23 15:26:46 +0100274static struct iommu_dev_data *get_dev_data(struct device *dev)
275{
276 return dev->archdata.iommu;
277}
278
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100279static bool pci_iommuv2_capable(struct pci_dev *pdev)
280{
281 static const int caps[] = {
282 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100283 PCI_EXT_CAP_ID_PRI,
284 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100285 };
286 int i, pos;
287
288 for (i = 0; i < 3; ++i) {
289 pos = pci_find_ext_capability(pdev, caps[i]);
290 if (pos == 0)
291 return false;
292 }
293
294 return true;
295}
296
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100297static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
298{
299 struct iommu_dev_data *dev_data;
300
301 dev_data = get_dev_data(&pdev->dev);
302
303 return dev_data->errata & (1 << erratum) ? true : false;
304}
305
Joerg Roedel71c70982009-11-24 16:43:06 +0100306/*
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200307 * This function actually applies the mapping to the page table of the
308 * dma_ops domain.
Joerg Roedel71c70982009-11-24 16:43:06 +0100309 */
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200310static void alloc_unity_mapping(struct dma_ops_domain *dma_dom,
311 struct unity_map_entry *e)
Joerg Roedel71c70982009-11-24 16:43:06 +0100312{
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200313 u64 addr;
Joerg Roedel71c70982009-11-24 16:43:06 +0100314
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200315 for (addr = e->address_start; addr < e->address_end;
316 addr += PAGE_SIZE) {
317 if (addr < dma_dom->aperture_size)
318 __set_bit(addr >> PAGE_SHIFT,
319 dma_dom->aperture[0]->bitmap);
Joerg Roedel71c70982009-11-24 16:43:06 +0100320 }
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200321}
Joerg Roedel71c70982009-11-24 16:43:06 +0100322
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200323/*
324 * Inits the unity mappings required for a specific device
325 */
326static void init_unity_mappings_for_device(struct device *dev,
327 struct dma_ops_domain *dma_dom)
328{
329 struct unity_map_entry *e;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400330 int devid;
Joerg Roedel71c70982009-11-24 16:43:06 +0100331
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200332 devid = get_device_id(dev);
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400333 if (IS_ERR_VALUE(devid))
334 return;
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200335
336 list_for_each_entry(e, &amd_iommu_unity_map, list) {
337 if (!(devid >= e->devid_start && devid <= e->devid_end))
338 continue;
339 alloc_unity_mapping(dma_dom, e);
340 }
Joerg Roedel71c70982009-11-24 16:43:06 +0100341}
342
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100343/*
344 * This function checks if the driver got a valid device from the caller to
345 * avoid dereferencing invalid pointers.
346 */
347static bool check_device(struct device *dev)
348{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400349 int devid;
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100350
351 if (!dev || !dev->dma_mask)
352 return false;
353
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100354 devid = get_device_id(dev);
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400355 if (IS_ERR_VALUE(devid))
356 return false;
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100357
358 /* Out of our scope? */
359 if (devid > amd_iommu_last_bdf)
360 return false;
361
362 if (amd_iommu_rlookup_table[devid] == NULL)
363 return false;
364
365 return true;
366}
367
Alex Williamson25b11ce2014-09-19 10:03:13 -0600368static void init_iommu_group(struct device *dev)
Alex Williamson2851db22012-10-08 22:49:41 -0600369{
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200370 struct dma_ops_domain *dma_domain;
371 struct iommu_domain *domain;
Alex Williamson2851db22012-10-08 22:49:41 -0600372 struct iommu_group *group;
Alex Williamson2851db22012-10-08 22:49:41 -0600373
Alex Williamson65d53522014-07-03 09:51:30 -0600374 group = iommu_group_get_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200375 if (IS_ERR(group))
376 return;
377
378 domain = iommu_group_default_domain(group);
379 if (!domain)
380 goto out;
381
382 dma_domain = to_pdomain(domain)->priv;
383
384 init_unity_mappings_for_device(dev, dma_domain);
385out:
386 iommu_group_put(group);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600387}
388
389static int iommu_init_device(struct device *dev)
390{
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600391 struct iommu_dev_data *dev_data;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400392 int devid;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600393
394 if (dev->archdata.iommu)
395 return 0;
396
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400397 devid = get_device_id(dev);
398 if (IS_ERR_VALUE(devid))
399 return devid;
400
401 dev_data = find_dev_data(devid);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600402 if (!dev_data)
403 return -ENOMEM;
404
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400405 if (dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100406 struct amd_iommu *iommu;
407
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400408 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100409 dev_data->iommu_v2 = iommu->is_iommu_v2;
410 }
411
Joerg Roedel657cbb62009-11-23 15:26:46 +0100412 dev->archdata.iommu = dev_data;
413
Alex Williamson066f2e92014-06-12 16:12:37 -0600414 iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
415 dev);
416
Joerg Roedel657cbb62009-11-23 15:26:46 +0100417 return 0;
418}
419
Joerg Roedel26018872011-06-06 16:50:14 +0200420static void iommu_ignore_device(struct device *dev)
421{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400422 u16 alias;
423 int devid;
Joerg Roedel26018872011-06-06 16:50:14 +0200424
425 devid = get_device_id(dev);
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400426 if (IS_ERR_VALUE(devid))
427 return;
428
Joerg Roedel26018872011-06-06 16:50:14 +0200429 alias = amd_iommu_alias_table[devid];
430
431 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
432 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
433
434 amd_iommu_rlookup_table[devid] = NULL;
435 amd_iommu_rlookup_table[alias] = NULL;
436}
437
Joerg Roedel657cbb62009-11-23 15:26:46 +0100438static void iommu_uninit_device(struct device *dev)
439{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400440 int devid;
441 struct iommu_dev_data *dev_data;
Alex Williamsonc1931092014-07-03 09:51:24 -0600442
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400443 devid = get_device_id(dev);
444 if (IS_ERR_VALUE(devid))
445 return;
446
447 dev_data = search_dev_data(devid);
Alex Williamsonc1931092014-07-03 09:51:24 -0600448 if (!dev_data)
449 return;
450
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100451 if (dev_data->domain)
452 detach_device(dev);
453
Alex Williamson066f2e92014-06-12 16:12:37 -0600454 iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
455 dev);
456
Alex Williamson9dcd6132012-05-30 14:19:07 -0600457 iommu_group_remove_device(dev);
458
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200459 /* Remove dma-ops */
460 dev->archdata.dma_ops = NULL;
461
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200462 /*
Alex Williamsonc1931092014-07-03 09:51:24 -0600463 * We keep dev_data around for unplugged devices and reuse it when the
464 * device is re-plugged - not doing so would introduce a ton of races.
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200465 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100466}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100467
Joerg Roedel7f265082008-12-12 13:50:21 +0100468#ifdef CONFIG_AMD_IOMMU_STATS
469
470/*
471 * Initialization code for statistics collection
472 */
473
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100474DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100475DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100476DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100477DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100478DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100479DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100480DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100481DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100482DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100483DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100484DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100485DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100486DECLARE_STATS_COUNTER(complete_ppr);
487DECLARE_STATS_COUNTER(invalidate_iotlb);
488DECLARE_STATS_COUNTER(invalidate_iotlb_all);
489DECLARE_STATS_COUNTER(pri_requests);
490
Joerg Roedel7f265082008-12-12 13:50:21 +0100491static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100492static struct dentry *de_fflush;
493
494static void amd_iommu_stats_add(struct __iommu_counter *cnt)
495{
496 if (stats_dir == NULL)
497 return;
498
499 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
500 &cnt->value);
501}
502
503static void amd_iommu_stats_init(void)
504{
505 stats_dir = debugfs_create_dir("amd-iommu", NULL);
506 if (stats_dir == NULL)
507 return;
508
Joerg Roedel7f265082008-12-12 13:50:21 +0100509 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300510 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100511
512 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100513 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100514 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100515 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100516 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100517 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100518 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100519 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100520 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100521 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100522 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100523 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100524 amd_iommu_stats_add(&complete_ppr);
525 amd_iommu_stats_add(&invalidate_iotlb);
526 amd_iommu_stats_add(&invalidate_iotlb_all);
527 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100528}
529
530#endif
531
Joerg Roedel431b2a22008-07-11 17:14:22 +0200532/****************************************************************************
533 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200534 * Interrupt handling functions
535 *
536 ****************************************************************************/
537
Joerg Roedele3e59872009-09-03 14:02:10 +0200538static void dump_dte_entry(u16 devid)
539{
540 int i;
541
Joerg Roedelee6c2862011-11-09 12:06:03 +0100542 for (i = 0; i < 4; ++i)
543 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200544 amd_iommu_dev_table[devid].data[i]);
545}
546
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200547static void dump_command(unsigned long phys_addr)
548{
549 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
550 int i;
551
552 for (i = 0; i < 4; ++i)
553 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
554}
555
Joerg Roedela345b232009-09-03 15:01:43 +0200556static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200557{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200558 int type, devid, domid, flags;
559 volatile u32 *event = __evt;
560 int count = 0;
561 u64 address;
562
563retry:
564 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
565 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
566 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
567 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
568 address = (u64)(((u64)event[3]) << 32) | event[2];
569
570 if (type == 0) {
571 /* Did we hit the erratum? */
572 if (++count == LOOP_TIMEOUT) {
573 pr_err("AMD-Vi: No event written to event log\n");
574 return;
575 }
576 udelay(1);
577 goto retry;
578 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200579
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200580 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200581
582 switch (type) {
583 case EVENT_TYPE_ILL_DEV:
584 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
585 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700586 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200587 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200588 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200589 break;
590 case EVENT_TYPE_IO_FAULT:
591 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
592 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700593 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200594 domid, address, flags);
595 break;
596 case EVENT_TYPE_DEV_TAB_ERR:
597 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
598 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700599 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200600 address, flags);
601 break;
602 case EVENT_TYPE_PAGE_TAB_ERR:
603 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
604 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700605 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200606 domid, address, flags);
607 break;
608 case EVENT_TYPE_ILL_CMD:
609 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200610 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200611 break;
612 case EVENT_TYPE_CMD_HARD_ERR:
613 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
614 "flags=0x%04x]\n", address, flags);
615 break;
616 case EVENT_TYPE_IOTLB_INV_TO:
617 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
618 "address=0x%016llx]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700619 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200620 address);
621 break;
622 case EVENT_TYPE_INV_DEV_REQ:
623 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
624 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700625 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200626 address, flags);
627 break;
628 default:
629 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
630 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200631
632 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200633}
634
635static void iommu_poll_events(struct amd_iommu *iommu)
636{
637 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200638
639 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
640 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
641
642 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200643 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200644 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200645 }
646
647 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200648}
649
Joerg Roedeleee53532012-06-01 15:20:23 +0200650static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100651{
652 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100653
Joerg Roedel399be2f2011-12-01 16:53:47 +0100654 INC_STATS_COUNTER(pri_requests);
655
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100656 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
657 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
658 return;
659 }
660
661 fault.address = raw[1];
662 fault.pasid = PPR_PASID(raw[0]);
663 fault.device_id = PPR_DEVID(raw[0]);
664 fault.tag = PPR_TAG(raw[0]);
665 fault.flags = PPR_FLAGS(raw[0]);
666
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100667 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
668}
669
670static void iommu_poll_ppr_log(struct amd_iommu *iommu)
671{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100672 u32 head, tail;
673
674 if (iommu->ppr_log == NULL)
675 return;
676
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100677 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
678 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
679
680 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200681 volatile u64 *raw;
682 u64 entry[2];
683 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100684
Joerg Roedeleee53532012-06-01 15:20:23 +0200685 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100686
Joerg Roedeleee53532012-06-01 15:20:23 +0200687 /*
688 * Hardware bug: Interrupt may arrive before the entry is
689 * written to memory. If this happens we need to wait for the
690 * entry to arrive.
691 */
692 for (i = 0; i < LOOP_TIMEOUT; ++i) {
693 if (PPR_REQ_TYPE(raw[0]) != 0)
694 break;
695 udelay(1);
696 }
697
698 /* Avoid memcpy function-call overhead */
699 entry[0] = raw[0];
700 entry[1] = raw[1];
701
702 /*
703 * To detect the hardware bug we need to clear the entry
704 * back to zero.
705 */
706 raw[0] = raw[1] = 0UL;
707
708 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100709 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
710 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200711
Joerg Roedeleee53532012-06-01 15:20:23 +0200712 /* Handle PPR entry */
713 iommu_handle_ppr_entry(iommu, entry);
714
Joerg Roedeleee53532012-06-01 15:20:23 +0200715 /* Refresh ring-buffer information */
716 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100717 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
718 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100719}
720
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200721irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200722{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500723 struct amd_iommu *iommu = (struct amd_iommu *) data;
724 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200725
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500726 while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
727 /* Enable EVT and PPR interrupts again */
728 writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
729 iommu->mmio_base + MMIO_STATUS_OFFSET);
730
731 if (status & MMIO_STATUS_EVT_INT_MASK) {
732 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
733 iommu_poll_events(iommu);
734 }
735
736 if (status & MMIO_STATUS_PPR_INT_MASK) {
737 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
738 iommu_poll_ppr_log(iommu);
739 }
740
741 /*
742 * Hardware bug: ERBT1312
743 * When re-enabling interrupt (by writing 1
744 * to clear the bit), the hardware might also try to set
745 * the interrupt bit in the event status register.
746 * In this scenario, the bit will be set, and disable
747 * subsequent interrupts.
748 *
749 * Workaround: The IOMMU driver should read back the
750 * status register and check if the interrupt bits are cleared.
751 * If not, driver will need to go through the interrupt handler
752 * again and re-clear the bits
753 */
754 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100755 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200756 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200757}
758
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200759irqreturn_t amd_iommu_int_handler(int irq, void *data)
760{
761 return IRQ_WAKE_THREAD;
762}
763
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200764/****************************************************************************
765 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200766 * IOMMU command queuing functions
767 *
768 ****************************************************************************/
769
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200770static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200771{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200772 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200773
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200774 while (*sem == 0 && i < LOOP_TIMEOUT) {
775 udelay(1);
776 i += 1;
777 }
778
779 if (i == LOOP_TIMEOUT) {
780 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
781 return -EIO;
782 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200783
784 return 0;
785}
786
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200787static void copy_cmd_to_buffer(struct amd_iommu *iommu,
788 struct iommu_cmd *cmd,
789 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200790{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200791 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200792
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200793 target = iommu->cmd_buf + tail;
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200794 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200795
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200796 /* Copy command to buffer */
797 memcpy(target, cmd, sizeof(*cmd));
798
799 /* Tell the IOMMU about it */
800 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
801}
802
Joerg Roedel815b33f2011-04-06 17:26:49 +0200803static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200804{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200805 WARN_ON(address & 0x7ULL);
806
Joerg Roedelded46732011-04-06 10:53:48 +0200807 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200808 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
809 cmd->data[1] = upper_32_bits(__pa(address));
810 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200811 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
812}
813
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200814static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
815{
816 memset(cmd, 0, sizeof(*cmd));
817 cmd->data[0] = devid;
818 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
819}
820
Joerg Roedel11b64022011-04-06 11:49:28 +0200821static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
822 size_t size, u16 domid, int pde)
823{
824 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100825 bool s;
Joerg Roedel11b64022011-04-06 11:49:28 +0200826
827 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100828 s = false;
Joerg Roedel11b64022011-04-06 11:49:28 +0200829
830 if (pages > 1) {
831 /*
832 * If we have to flush more than one page, flush all
833 * TLB entries for this domain
834 */
835 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100836 s = true;
Joerg Roedel11b64022011-04-06 11:49:28 +0200837 }
838
839 address &= PAGE_MASK;
840
841 memset(cmd, 0, sizeof(*cmd));
842 cmd->data[1] |= domid;
843 cmd->data[2] = lower_32_bits(address);
844 cmd->data[3] = upper_32_bits(address);
845 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
846 if (s) /* size bit - we flush more than one 4kb page */
847 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200848 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200849 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
850}
851
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200852static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
853 u64 address, size_t size)
854{
855 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100856 bool s;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200857
858 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100859 s = false;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200860
861 if (pages > 1) {
862 /*
863 * If we have to flush more than one page, flush all
864 * TLB entries for this domain
865 */
866 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100867 s = true;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200868 }
869
870 address &= PAGE_MASK;
871
872 memset(cmd, 0, sizeof(*cmd));
873 cmd->data[0] = devid;
874 cmd->data[0] |= (qdep & 0xff) << 24;
875 cmd->data[1] = devid;
876 cmd->data[2] = lower_32_bits(address);
877 cmd->data[3] = upper_32_bits(address);
878 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
879 if (s)
880 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
881}
882
Joerg Roedel22e266c2011-11-21 15:59:08 +0100883static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
884 u64 address, bool size)
885{
886 memset(cmd, 0, sizeof(*cmd));
887
888 address &= ~(0xfffULL);
889
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600890 cmd->data[0] = pasid;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100891 cmd->data[1] = domid;
892 cmd->data[2] = lower_32_bits(address);
893 cmd->data[3] = upper_32_bits(address);
894 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
895 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
896 if (size)
897 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
898 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
899}
900
901static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
902 int qdep, u64 address, bool size)
903{
904 memset(cmd, 0, sizeof(*cmd));
905
906 address &= ~(0xfffULL);
907
908 cmd->data[0] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600909 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100910 cmd->data[0] |= (qdep & 0xff) << 24;
911 cmd->data[1] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600912 cmd->data[1] |= (pasid & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100913 cmd->data[2] = lower_32_bits(address);
914 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
915 cmd->data[3] = upper_32_bits(address);
916 if (size)
917 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
918 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
919}
920
Joerg Roedelc99afa22011-11-21 18:19:25 +0100921static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
922 int status, int tag, bool gn)
923{
924 memset(cmd, 0, sizeof(*cmd));
925
926 cmd->data[0] = devid;
927 if (gn) {
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600928 cmd->data[1] = pasid;
Joerg Roedelc99afa22011-11-21 18:19:25 +0100929 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
930 }
931 cmd->data[3] = tag & 0x1ff;
932 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
933
934 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
935}
936
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200937static void build_inv_all(struct iommu_cmd *cmd)
938{
939 memset(cmd, 0, sizeof(*cmd));
940 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200941}
942
Joerg Roedel7ef27982012-06-21 16:46:04 +0200943static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
944{
945 memset(cmd, 0, sizeof(*cmd));
946 cmd->data[0] = devid;
947 CMD_SET_TYPE(cmd, CMD_INV_IRT);
948}
949
Joerg Roedel431b2a22008-07-11 17:14:22 +0200950/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200951 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200952 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200953 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200954static int iommu_queue_command_sync(struct amd_iommu *iommu,
955 struct iommu_cmd *cmd,
956 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200957{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200958 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200959 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200960
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200961again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200962 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200963
964 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
965 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200966 next_tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
967 left = (head - next_tail) % CMD_BUFFER_SIZE;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200968
969 if (left <= 2) {
970 struct iommu_cmd sync_cmd;
971 volatile u64 sem = 0;
972 int ret;
973
974 build_completion_wait(&sync_cmd, (u64)&sem);
975 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
976
977 spin_unlock_irqrestore(&iommu->lock, flags);
978
979 if ((ret = wait_on_sem(&sem)) != 0)
980 return ret;
981
982 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200983 }
984
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200985 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200986
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200987 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200988 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200989
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200990 spin_unlock_irqrestore(&iommu->lock, flags);
991
Joerg Roedel815b33f2011-04-06 17:26:49 +0200992 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100993}
994
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200995static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
996{
997 return iommu_queue_command_sync(iommu, cmd, true);
998}
999
Joerg Roedel8d201962008-12-02 20:34:41 +01001000/*
1001 * This function queues a completion wait command into the command
1002 * buffer of an IOMMU
1003 */
Joerg Roedel8d201962008-12-02 20:34:41 +01001004static int iommu_completion_wait(struct amd_iommu *iommu)
1005{
Joerg Roedel815b33f2011-04-06 17:26:49 +02001006 struct iommu_cmd cmd;
1007 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001008 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001009
1010 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001011 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001012
Joerg Roedel815b33f2011-04-06 17:26:49 +02001013 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +01001014
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001015 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +01001016 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001017 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001018
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001019 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001020}
1021
Joerg Roedeld8c13082011-04-06 18:51:26 +02001022static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001023{
1024 struct iommu_cmd cmd;
1025
Joerg Roedeld8c13082011-04-06 18:51:26 +02001026 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001027
Joerg Roedeld8c13082011-04-06 18:51:26 +02001028 return iommu_queue_command(iommu, &cmd);
1029}
1030
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001031static void iommu_flush_dte_all(struct amd_iommu *iommu)
1032{
1033 u32 devid;
1034
1035 for (devid = 0; devid <= 0xffff; ++devid)
1036 iommu_flush_dte(iommu, devid);
1037
1038 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001039}
1040
1041/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001042 * This function uses heavy locking and may disable irqs for some time. But
1043 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001044 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001045static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001046{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001047 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001048
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001049 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1050 struct iommu_cmd cmd;
1051 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1052 dom_id, 1);
1053 iommu_queue_command(iommu, &cmd);
1054 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001055
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001056 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001057}
1058
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001059static void iommu_flush_all(struct amd_iommu *iommu)
1060{
1061 struct iommu_cmd cmd;
1062
1063 build_inv_all(&cmd);
1064
1065 iommu_queue_command(iommu, &cmd);
1066 iommu_completion_wait(iommu);
1067}
1068
Joerg Roedel7ef27982012-06-21 16:46:04 +02001069static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1070{
1071 struct iommu_cmd cmd;
1072
1073 build_inv_irt(&cmd, devid);
1074
1075 iommu_queue_command(iommu, &cmd);
1076}
1077
1078static void iommu_flush_irt_all(struct amd_iommu *iommu)
1079{
1080 u32 devid;
1081
1082 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1083 iommu_flush_irt(iommu, devid);
1084
1085 iommu_completion_wait(iommu);
1086}
1087
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001088void iommu_flush_all_caches(struct amd_iommu *iommu)
1089{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001090 if (iommu_feature(iommu, FEATURE_IA)) {
1091 iommu_flush_all(iommu);
1092 } else {
1093 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001094 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001095 iommu_flush_tlb_all(iommu);
1096 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001097}
1098
Joerg Roedel431b2a22008-07-11 17:14:22 +02001099/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001100 * Command send function for flushing on-device TLB
1101 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001102static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1103 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001104{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001105 struct amd_iommu *iommu;
1106 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001107 int qdep;
1108
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001109 qdep = dev_data->ats.qdep;
1110 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001111
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001112 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001113
1114 return iommu_queue_command(iommu, &cmd);
1115}
1116
1117/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001118 * Command send function for invalidating a device table entry
1119 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001120static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001121{
1122 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001123 u16 alias;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001124 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001125
Joerg Roedel6c542042011-06-09 17:07:31 +02001126 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele25bfb52015-10-20 17:33:38 +02001127 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001128
Joerg Roedelf62dda62011-06-09 12:55:35 +02001129 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001130 if (!ret && alias != dev_data->devid)
1131 ret = iommu_flush_dte(iommu, alias);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001132 if (ret)
1133 return ret;
1134
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001135 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001136 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001137
1138 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001139}
1140
Joerg Roedel431b2a22008-07-11 17:14:22 +02001141/*
1142 * TLB invalidation function which is called from the mapping functions.
1143 * It invalidates a single PTE if the range to flush is within a single
1144 * page. Otherwise it flushes the whole TLB of the IOMMU.
1145 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001146static void __domain_flush_pages(struct protection_domain *domain,
1147 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001148{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001149 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001150 struct iommu_cmd cmd;
1151 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001152
Joerg Roedel11b64022011-04-06 11:49:28 +02001153 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001154
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001155 for (i = 0; i < amd_iommus_present; ++i) {
1156 if (!domain->dev_iommu[i])
1157 continue;
1158
1159 /*
1160 * Devices of this domain are behind this IOMMU
1161 * We need a TLB flush
1162 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001163 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001164 }
1165
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001166 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001167
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001168 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001169 continue;
1170
Joerg Roedel6c542042011-06-09 17:07:31 +02001171 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001172 }
1173
Joerg Roedel11b64022011-04-06 11:49:28 +02001174 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001175}
1176
Joerg Roedel17b124b2011-04-06 18:01:35 +02001177static void domain_flush_pages(struct protection_domain *domain,
1178 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001179{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001180 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001181}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001182
Joerg Roedel1c655772008-09-04 18:40:05 +02001183/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001184static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001185{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001186 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001187}
1188
Chris Wright42a49f92009-06-15 15:42:00 +02001189/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001190static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001191{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001192 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1193}
1194
1195static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001196{
1197 int i;
1198
1199 for (i = 0; i < amd_iommus_present; ++i) {
1200 if (!domain->dev_iommu[i])
1201 continue;
1202
1203 /*
1204 * Devices of this domain are behind this IOMMU
1205 * We need to wait for completion of all commands.
1206 */
1207 iommu_completion_wait(amd_iommus[i]);
1208 }
1209}
1210
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001211
Joerg Roedel43f49602008-12-02 21:01:12 +01001212/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001213 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001214 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001215static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001216{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001217 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001218
1219 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001220 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001221}
1222
Joerg Roedel431b2a22008-07-11 17:14:22 +02001223/****************************************************************************
1224 *
1225 * The functions below are used the create the page table mappings for
1226 * unity mapped regions.
1227 *
1228 ****************************************************************************/
1229
1230/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001231 * This function is used to add another level to an IO page table. Adding
1232 * another level increases the size of the address space by 9 bits to a size up
1233 * to 64 bits.
1234 */
1235static bool increase_address_space(struct protection_domain *domain,
1236 gfp_t gfp)
1237{
1238 u64 *pte;
1239
1240 if (domain->mode == PAGE_MODE_6_LEVEL)
1241 /* address space already 64 bit large */
1242 return false;
1243
1244 pte = (void *)get_zeroed_page(gfp);
1245 if (!pte)
1246 return false;
1247
1248 *pte = PM_LEVEL_PDE(domain->mode,
1249 virt_to_phys(domain->pt_root));
1250 domain->pt_root = pte;
1251 domain->mode += 1;
1252 domain->updated = true;
1253
1254 return true;
1255}
1256
1257static u64 *alloc_pte(struct protection_domain *domain,
1258 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001259 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001260 u64 **pte_page,
1261 gfp_t gfp)
1262{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001263 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001264 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001265
1266 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001267
1268 while (address > PM_LEVEL_SIZE(domain->mode))
1269 increase_address_space(domain, gfp);
1270
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001271 level = domain->mode - 1;
1272 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1273 address = PAGE_SIZE_ALIGN(address, page_size);
1274 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001275
1276 while (level > end_lvl) {
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001277 u64 __pte, __npte;
1278
1279 __pte = *pte;
1280
1281 if (!IOMMU_PTE_PRESENT(__pte)) {
Joerg Roedel308973d2009-11-24 17:43:32 +01001282 page = (u64 *)get_zeroed_page(gfp);
1283 if (!page)
1284 return NULL;
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001285
1286 __npte = PM_LEVEL_PDE(level, virt_to_phys(page));
1287
1288 if (cmpxchg64(pte, __pte, __npte)) {
1289 free_page((unsigned long)page);
1290 continue;
1291 }
Joerg Roedel308973d2009-11-24 17:43:32 +01001292 }
1293
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001294 /* No level skipping support yet */
1295 if (PM_PTE_LEVEL(*pte) != level)
1296 return NULL;
1297
Joerg Roedel308973d2009-11-24 17:43:32 +01001298 level -= 1;
1299
1300 pte = IOMMU_PTE_PAGE(*pte);
1301
1302 if (pte_page && level == end_lvl)
1303 *pte_page = pte;
1304
1305 pte = &pte[PM_LEVEL_INDEX(level, address)];
1306 }
1307
1308 return pte;
1309}
1310
1311/*
1312 * This function checks if there is a PTE for a given dma address. If
1313 * there is one, it returns the pointer to it.
1314 */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001315static u64 *fetch_pte(struct protection_domain *domain,
1316 unsigned long address,
1317 unsigned long *page_size)
Joerg Roedel308973d2009-11-24 17:43:32 +01001318{
1319 int level;
1320 u64 *pte;
1321
Joerg Roedel24cd7722010-01-19 17:27:39 +01001322 if (address > PM_LEVEL_SIZE(domain->mode))
1323 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001324
Joerg Roedel3039ca12015-04-01 14:58:48 +02001325 level = domain->mode - 1;
1326 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1327 *page_size = PTE_LEVEL_PAGE_SIZE(level);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001328
1329 while (level > 0) {
1330
1331 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001332 if (!IOMMU_PTE_PRESENT(*pte))
1333 return NULL;
1334
Joerg Roedel24cd7722010-01-19 17:27:39 +01001335 /* Large PTE */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001336 if (PM_PTE_LEVEL(*pte) == 7 ||
1337 PM_PTE_LEVEL(*pte) == 0)
1338 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001339
1340 /* No level skipping support yet */
1341 if (PM_PTE_LEVEL(*pte) != level)
1342 return NULL;
1343
Joerg Roedel308973d2009-11-24 17:43:32 +01001344 level -= 1;
1345
Joerg Roedel24cd7722010-01-19 17:27:39 +01001346 /* Walk to the next level */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001347 pte = IOMMU_PTE_PAGE(*pte);
1348 pte = &pte[PM_LEVEL_INDEX(level, address)];
1349 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1350 }
1351
1352 if (PM_PTE_LEVEL(*pte) == 0x07) {
1353 unsigned long pte_mask;
1354
1355 /*
1356 * If we have a series of large PTEs, make
1357 * sure to return a pointer to the first one.
1358 */
1359 *page_size = pte_mask = PTE_PAGE_SIZE(*pte);
1360 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1361 pte = (u64 *)(((unsigned long)pte) & pte_mask);
Joerg Roedel308973d2009-11-24 17:43:32 +01001362 }
1363
1364 return pte;
1365}
1366
1367/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001368 * Generic mapping functions. It maps a physical address into a DMA
1369 * address space. It allocates the page table pages if necessary.
1370 * In the future it can be extended to a generic mapping function
1371 * supporting all features of AMD IOMMU page tables like level skipping
1372 * and full 64 bit address spaces.
1373 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001374static int iommu_map_page(struct protection_domain *dom,
1375 unsigned long bus_addr,
1376 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001377 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001378 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001379{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001380 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001381 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001382
Joerg Roedeld4b03662015-04-01 14:58:52 +02001383 BUG_ON(!IS_ALIGNED(bus_addr, page_size));
1384 BUG_ON(!IS_ALIGNED(phys_addr, page_size));
1385
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001386 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001387 return -EINVAL;
1388
Joerg Roedeld4b03662015-04-01 14:58:52 +02001389 count = PAGE_SIZE_PTE_COUNT(page_size);
1390 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001391
Maurizio Lombardi63eaa752014-09-11 12:28:03 +02001392 if (!pte)
1393 return -ENOMEM;
1394
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001395 for (i = 0; i < count; ++i)
1396 if (IOMMU_PTE_PRESENT(pte[i]))
1397 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001398
Joerg Roedeld4b03662015-04-01 14:58:52 +02001399 if (count > 1) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001400 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1401 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1402 } else
1403 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1404
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001405 if (prot & IOMMU_PROT_IR)
1406 __pte |= IOMMU_PTE_IR;
1407 if (prot & IOMMU_PROT_IW)
1408 __pte |= IOMMU_PTE_IW;
1409
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001410 for (i = 0; i < count; ++i)
1411 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001412
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001413 update_domain(dom);
1414
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001415 return 0;
1416}
1417
Joerg Roedel24cd7722010-01-19 17:27:39 +01001418static unsigned long iommu_unmap_page(struct protection_domain *dom,
1419 unsigned long bus_addr,
1420 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001421{
Joerg Roedel71b390e2015-04-01 14:58:49 +02001422 unsigned long long unmapped;
1423 unsigned long unmap_size;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001424 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001425
Joerg Roedel24cd7722010-01-19 17:27:39 +01001426 BUG_ON(!is_power_of_2(page_size));
1427
1428 unmapped = 0;
1429
1430 while (unmapped < page_size) {
1431
Joerg Roedel71b390e2015-04-01 14:58:49 +02001432 pte = fetch_pte(dom, bus_addr, &unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001433
Joerg Roedel71b390e2015-04-01 14:58:49 +02001434 if (pte) {
1435 int i, count;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001436
Joerg Roedel71b390e2015-04-01 14:58:49 +02001437 count = PAGE_SIZE_PTE_COUNT(unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001438 for (i = 0; i < count; i++)
1439 pte[i] = 0ULL;
1440 }
1441
1442 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1443 unmapped += unmap_size;
1444 }
1445
Alex Williamson60d0ca32013-06-21 14:33:19 -06001446 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001447
1448 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001449}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001450
Joerg Roedel431b2a22008-07-11 17:14:22 +02001451/****************************************************************************
1452 *
1453 * The next functions belong to the address allocator for the dma_ops
1454 * interface functions. They work like the allocators in the other IOMMU
1455 * drivers. Its basically a bitmap which marks the allocated pages in
1456 * the aperture. Maybe it could be enhanced in the future to a more
1457 * efficient allocator.
1458 *
1459 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001460
Joerg Roedel431b2a22008-07-11 17:14:22 +02001461/*
Joerg Roedel384de722009-05-15 12:30:05 +02001462 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001463 *
1464 * called with domain->lock held
1465 */
Joerg Roedel384de722009-05-15 12:30:05 +02001466
Joerg Roedel9cabe892009-05-18 16:38:55 +02001467/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001468 * Used to reserve address ranges in the aperture (e.g. for exclusion
1469 * ranges.
1470 */
1471static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1472 unsigned long start_page,
1473 unsigned int pages)
1474{
1475 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1476
1477 if (start_page + pages > last_page)
1478 pages = last_page - start_page;
1479
1480 for (i = start_page; i < start_page + pages; ++i) {
1481 int index = i / APERTURE_RANGE_PAGES;
1482 int page = i % APERTURE_RANGE_PAGES;
1483 __set_bit(page, dom->aperture[index]->bitmap);
1484 }
1485}
1486
1487/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001488 * This function is used to add a new aperture range to an existing
1489 * aperture in case of dma_ops domain allocation or address allocation
1490 * failure.
1491 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001492static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001493 bool populate, gfp_t gfp)
1494{
1495 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001496 unsigned long i, old_size, pte_pgsize;
Joerg Roedela73c1562015-12-21 19:25:56 +01001497 struct aperture_range *range;
1498 struct amd_iommu *iommu;
1499 unsigned long flags;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001500
Joerg Roedelf5e97052009-05-22 12:31:53 +02001501#ifdef CONFIG_IOMMU_STRESS
1502 populate = false;
1503#endif
1504
Joerg Roedel9cabe892009-05-18 16:38:55 +02001505 if (index >= APERTURE_MAX_RANGES)
1506 return -ENOMEM;
1507
Joerg Roedela73c1562015-12-21 19:25:56 +01001508 range = kzalloc(sizeof(struct aperture_range), gfp);
1509 if (!range)
Joerg Roedel9cabe892009-05-18 16:38:55 +02001510 return -ENOMEM;
1511
Joerg Roedela73c1562015-12-21 19:25:56 +01001512 range->bitmap = (void *)get_zeroed_page(gfp);
1513 if (!range->bitmap)
Joerg Roedel9cabe892009-05-18 16:38:55 +02001514 goto out_free;
1515
Joerg Roedela73c1562015-12-21 19:25:56 +01001516 range->offset = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001517
Joerg Roedela73c1562015-12-21 19:25:56 +01001518 spin_lock_init(&range->bitmap_lock);
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001519
Joerg Roedel9cabe892009-05-18 16:38:55 +02001520 if (populate) {
1521 unsigned long address = dma_dom->aperture_size;
1522 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1523 u64 *pte, *pte_page;
1524
1525 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001526 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001527 &pte_page, gfp);
1528 if (!pte)
1529 goto out_free;
1530
Joerg Roedela73c1562015-12-21 19:25:56 +01001531 range->pte_pages[i] = pte_page;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001532
1533 address += APERTURE_RANGE_SIZE / 64;
1534 }
1535 }
1536
Joerg Roedel92d420e2015-12-21 19:31:33 +01001537 spin_lock_irqsave(&dma_dom->domain.lock, flags);
1538
Joerg Roedela73c1562015-12-21 19:25:56 +01001539 /* First take the bitmap_lock and then publish the range */
Joerg Roedel92d420e2015-12-21 19:31:33 +01001540 spin_lock(&range->bitmap_lock);
Joerg Roedela73c1562015-12-21 19:25:56 +01001541
1542 old_size = dma_dom->aperture_size;
1543 dma_dom->aperture[index] = range;
1544 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001545
Joerg Roedel17f5b562011-07-06 17:14:44 +02001546 /* Reserve address range used for MSI messages */
1547 if (old_size < MSI_ADDR_BASE_LO &&
1548 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1549 unsigned long spage;
1550 int pages;
1551
1552 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1553 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1554
1555 dma_ops_reserve_addresses(dma_dom, spage, pages);
1556 }
1557
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001558 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001559 for_each_iommu(iommu) {
1560 if (iommu->exclusion_start &&
1561 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1562 && iommu->exclusion_start < dma_dom->aperture_size) {
1563 unsigned long startpage;
1564 int pages = iommu_num_pages(iommu->exclusion_start,
1565 iommu->exclusion_length,
1566 PAGE_SIZE);
1567 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1568 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1569 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001570 }
1571
1572 /*
1573 * Check for areas already mapped as present in the new aperture
1574 * range and mark those pages as reserved in the allocator. Such
1575 * mappings may already exist as a result of requested unity
1576 * mappings for devices.
1577 */
1578 for (i = dma_dom->aperture[index]->offset;
1579 i < dma_dom->aperture_size;
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001580 i += pte_pgsize) {
Joerg Roedel3039ca12015-04-01 14:58:48 +02001581 u64 *pte = fetch_pte(&dma_dom->domain, i, &pte_pgsize);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001582 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1583 continue;
1584
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001585 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT,
1586 pte_pgsize >> 12);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001587 }
1588
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001589 update_domain(&dma_dom->domain);
1590
Joerg Roedel92d420e2015-12-21 19:31:33 +01001591 spin_unlock(&range->bitmap_lock);
1592
1593 spin_unlock_irqrestore(&dma_dom->domain.lock, flags);
Joerg Roedela73c1562015-12-21 19:25:56 +01001594
Joerg Roedel9cabe892009-05-18 16:38:55 +02001595 return 0;
1596
1597out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001598 update_domain(&dma_dom->domain);
1599
Joerg Roedela73c1562015-12-21 19:25:56 +01001600 free_page((unsigned long)range->bitmap);
Joerg Roedel9cabe892009-05-18 16:38:55 +02001601
Joerg Roedela73c1562015-12-21 19:25:56 +01001602 kfree(range);
Joerg Roedel9cabe892009-05-18 16:38:55 +02001603
1604 return -ENOMEM;
1605}
1606
Joerg Roedelccb50e02015-12-21 17:49:34 +01001607static dma_addr_t dma_ops_aperture_alloc(struct dma_ops_domain *dom,
1608 struct aperture_range *range,
Joerg Roedela0f51442015-12-21 16:20:09 +01001609 unsigned long pages,
Joerg Roedela0f51442015-12-21 16:20:09 +01001610 unsigned long dma_mask,
1611 unsigned long boundary_size,
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001612 unsigned long align_mask,
1613 bool trylock)
Joerg Roedela0f51442015-12-21 16:20:09 +01001614{
1615 unsigned long offset, limit, flags;
1616 dma_addr_t address;
Joerg Roedelccb50e02015-12-21 17:49:34 +01001617 bool flush = false;
Joerg Roedela0f51442015-12-21 16:20:09 +01001618
1619 offset = range->offset >> PAGE_SHIFT;
1620 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1621 dma_mask >> PAGE_SHIFT);
1622
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001623 if (trylock) {
1624 if (!spin_trylock_irqsave(&range->bitmap_lock, flags))
1625 return -1;
1626 } else {
1627 spin_lock_irqsave(&range->bitmap_lock, flags);
1628 }
1629
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001630 address = iommu_area_alloc(range->bitmap, limit, range->next_bit,
1631 pages, offset, boundary_size, align_mask);
Joerg Roedelccb50e02015-12-21 17:49:34 +01001632 if (address == -1) {
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001633 /* Nothing found, retry one time */
1634 address = iommu_area_alloc(range->bitmap, limit,
1635 0, pages, offset, boundary_size,
1636 align_mask);
Joerg Roedelccb50e02015-12-21 17:49:34 +01001637 flush = true;
1638 }
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001639
1640 if (address != -1)
1641 range->next_bit = address + pages;
1642
Joerg Roedela0f51442015-12-21 16:20:09 +01001643 spin_unlock_irqrestore(&range->bitmap_lock, flags);
1644
Joerg Roedelccb50e02015-12-21 17:49:34 +01001645 if (flush) {
1646 domain_flush_tlb(&dom->domain);
1647 domain_flush_complete(&dom->domain);
1648 }
1649
Joerg Roedela0f51442015-12-21 16:20:09 +01001650 return address;
1651}
1652
Joerg Roedel384de722009-05-15 12:30:05 +02001653static unsigned long dma_ops_area_alloc(struct device *dev,
1654 struct dma_ops_domain *dom,
1655 unsigned int pages,
1656 unsigned long align_mask,
Joerg Roedel05ab49e2015-12-21 17:58:26 +01001657 u64 dma_mask)
Joerg Roedel384de722009-05-15 12:30:05 +02001658{
Joerg Roedelab7032b2015-12-21 18:47:11 +01001659 unsigned long boundary_size, mask;
Joerg Roedel384de722009-05-15 12:30:05 +02001660 unsigned long address = -1;
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001661 bool first = true;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001662 u32 start, i;
1663
1664 preempt_disable();
Joerg Roedel384de722009-05-15 12:30:05 +02001665
Joerg Roedele6aabee2015-05-27 09:26:09 +02001666 mask = dma_get_seg_boundary(dev);
1667
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001668again:
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001669 start = this_cpu_read(*dom->next_index);
1670
1671 /* Sanity check - is it really necessary? */
1672 if (unlikely(start > APERTURE_MAX_RANGES)) {
1673 start = 0;
1674 this_cpu_write(*dom->next_index, 0);
1675 }
1676
Joerg Roedele6aabee2015-05-27 09:26:09 +02001677 boundary_size = mask + 1 ? ALIGN(mask + 1, PAGE_SIZE) >> PAGE_SHIFT :
1678 1UL << (BITS_PER_LONG - PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001679
Joerg Roedel2a874422015-12-21 18:34:47 +01001680 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1681 struct aperture_range *range;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001682 int index;
Joerg Roedelccb50e02015-12-21 17:49:34 +01001683
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001684 index = (start + i) % APERTURE_MAX_RANGES;
1685
1686 range = dom->aperture[index];
Joerg Roedel2a874422015-12-21 18:34:47 +01001687
1688 if (!range || range->offset >= dma_mask)
1689 continue;
Joerg Roedel384de722009-05-15 12:30:05 +02001690
Joerg Roedel2a874422015-12-21 18:34:47 +01001691 address = dma_ops_aperture_alloc(dom, range, pages,
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001692 dma_mask, boundary_size,
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001693 align_mask, first);
Joerg Roedel384de722009-05-15 12:30:05 +02001694 if (address != -1) {
Joerg Roedel2a874422015-12-21 18:34:47 +01001695 address = range->offset + (address << PAGE_SHIFT);
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001696 this_cpu_write(*dom->next_index, index);
Joerg Roedel384de722009-05-15 12:30:05 +02001697 break;
1698 }
Joerg Roedel384de722009-05-15 12:30:05 +02001699 }
1700
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001701 if (address == -1 && first) {
1702 first = false;
1703 goto again;
1704 }
1705
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001706 preempt_enable();
1707
Joerg Roedel384de722009-05-15 12:30:05 +02001708 return address;
1709}
1710
Joerg Roedeld3086442008-06-26 21:27:57 +02001711static unsigned long dma_ops_alloc_addresses(struct device *dev,
1712 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001713 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001714 unsigned long align_mask,
1715 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001716{
Joerg Roedel266a3bd2015-12-21 18:54:24 +01001717 unsigned long address = -1;
Joerg Roedeld3086442008-06-26 21:27:57 +02001718
Joerg Roedel266a3bd2015-12-21 18:54:24 +01001719 while (address == -1) {
1720 address = dma_ops_area_alloc(dev, dom, pages,
1721 align_mask, dma_mask);
1722
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001723 if (address == -1 && alloc_new_range(dom, false, GFP_ATOMIC))
Joerg Roedel266a3bd2015-12-21 18:54:24 +01001724 break;
1725 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001726
Joerg Roedel384de722009-05-15 12:30:05 +02001727 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001728 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001729
1730 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1731
1732 return address;
1733}
1734
Joerg Roedel431b2a22008-07-11 17:14:22 +02001735/*
1736 * The address free function.
1737 *
1738 * called with domain->lock held
1739 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001740static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1741 unsigned long address,
1742 unsigned int pages)
1743{
Joerg Roedel384de722009-05-15 12:30:05 +02001744 unsigned i = address >> APERTURE_RANGE_SHIFT;
1745 struct aperture_range *range = dom->aperture[i];
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001746 unsigned long flags;
Joerg Roedel80be3082008-11-06 14:59:05 +01001747
Joerg Roedel384de722009-05-15 12:30:05 +02001748 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1749
Joerg Roedel47bccd62009-05-22 12:40:54 +02001750#ifdef CONFIG_IOMMU_STRESS
1751 if (i < 4)
1752 return;
1753#endif
1754
Joerg Roedel4eeca8c2015-12-22 12:15:35 +01001755 if (amd_iommu_unmap_flush) {
Joerg Roedeld41ab092015-12-21 18:20:03 +01001756 domain_flush_tlb(&dom->domain);
1757 domain_flush_complete(&dom->domain);
1758 }
Joerg Roedel384de722009-05-15 12:30:05 +02001759
1760 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001761
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001762 spin_lock_irqsave(&range->bitmap_lock, flags);
Joerg Roedel4eeca8c2015-12-22 12:15:35 +01001763 if (address + pages > range->next_bit)
1764 range->next_bit = address + pages;
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001765 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel08c5fb92015-12-21 13:04:49 +01001766 spin_unlock_irqrestore(&range->bitmap_lock, flags);
Joerg Roedel384de722009-05-15 12:30:05 +02001767
Joerg Roedeld3086442008-06-26 21:27:57 +02001768}
1769
Joerg Roedel431b2a22008-07-11 17:14:22 +02001770/****************************************************************************
1771 *
1772 * The next functions belong to the domain allocation. A domain is
1773 * allocated for every IOMMU as the default domain. If device isolation
1774 * is enabled, every device get its own domain. The most important thing
1775 * about domains is the page table mapping the DMA address space they
1776 * contain.
1777 *
1778 ****************************************************************************/
1779
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001780/*
1781 * This function adds a protection domain to the global protection domain list
1782 */
1783static void add_domain_to_list(struct protection_domain *domain)
1784{
1785 unsigned long flags;
1786
1787 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1788 list_add(&domain->list, &amd_iommu_pd_list);
1789 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1790}
1791
1792/*
1793 * This function removes a protection domain to the global
1794 * protection domain list
1795 */
1796static void del_domain_from_list(struct protection_domain *domain)
1797{
1798 unsigned long flags;
1799
1800 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1801 list_del(&domain->list);
1802 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1803}
1804
Joerg Roedelec487d12008-06-26 21:27:58 +02001805static u16 domain_id_alloc(void)
1806{
1807 unsigned long flags;
1808 int id;
1809
1810 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1811 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1812 BUG_ON(id == 0);
1813 if (id > 0 && id < MAX_DOMAIN_ID)
1814 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1815 else
1816 id = 0;
1817 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1818
1819 return id;
1820}
1821
Joerg Roedela2acfb72008-12-02 18:28:53 +01001822static void domain_id_free(int id)
1823{
1824 unsigned long flags;
1825
1826 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1827 if (id > 0 && id < MAX_DOMAIN_ID)
1828 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1829 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1830}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001831
Joerg Roedel5c34c402013-06-20 20:22:58 +02001832#define DEFINE_FREE_PT_FN(LVL, FN) \
1833static void free_pt_##LVL (unsigned long __pt) \
1834{ \
1835 unsigned long p; \
1836 u64 *pt; \
1837 int i; \
1838 \
1839 pt = (u64 *)__pt; \
1840 \
1841 for (i = 0; i < 512; ++i) { \
Joerg Roedel0b3fff542015-06-18 10:48:34 +02001842 /* PTE present? */ \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001843 if (!IOMMU_PTE_PRESENT(pt[i])) \
1844 continue; \
1845 \
Joerg Roedel0b3fff542015-06-18 10:48:34 +02001846 /* Large PTE? */ \
1847 if (PM_PTE_LEVEL(pt[i]) == 0 || \
1848 PM_PTE_LEVEL(pt[i]) == 7) \
1849 continue; \
1850 \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001851 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1852 FN(p); \
1853 } \
1854 free_page((unsigned long)pt); \
1855}
1856
1857DEFINE_FREE_PT_FN(l2, free_page)
1858DEFINE_FREE_PT_FN(l3, free_pt_l2)
1859DEFINE_FREE_PT_FN(l4, free_pt_l3)
1860DEFINE_FREE_PT_FN(l5, free_pt_l4)
1861DEFINE_FREE_PT_FN(l6, free_pt_l5)
1862
Joerg Roedel86db2e52008-12-02 18:20:21 +01001863static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001864{
Joerg Roedel5c34c402013-06-20 20:22:58 +02001865 unsigned long root = (unsigned long)domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001866
Joerg Roedel5c34c402013-06-20 20:22:58 +02001867 switch (domain->mode) {
1868 case PAGE_MODE_NONE:
1869 break;
1870 case PAGE_MODE_1_LEVEL:
1871 free_page(root);
1872 break;
1873 case PAGE_MODE_2_LEVEL:
1874 free_pt_l2(root);
1875 break;
1876 case PAGE_MODE_3_LEVEL:
1877 free_pt_l3(root);
1878 break;
1879 case PAGE_MODE_4_LEVEL:
1880 free_pt_l4(root);
1881 break;
1882 case PAGE_MODE_5_LEVEL:
1883 free_pt_l5(root);
1884 break;
1885 case PAGE_MODE_6_LEVEL:
1886 free_pt_l6(root);
1887 break;
1888 default:
1889 BUG();
Joerg Roedelec487d12008-06-26 21:27:58 +02001890 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001891}
1892
Joerg Roedelb16137b2011-11-21 16:50:23 +01001893static void free_gcr3_tbl_level1(u64 *tbl)
1894{
1895 u64 *ptr;
1896 int i;
1897
1898 for (i = 0; i < 512; ++i) {
1899 if (!(tbl[i] & GCR3_VALID))
1900 continue;
1901
1902 ptr = __va(tbl[i] & PAGE_MASK);
1903
1904 free_page((unsigned long)ptr);
1905 }
1906}
1907
1908static void free_gcr3_tbl_level2(u64 *tbl)
1909{
1910 u64 *ptr;
1911 int i;
1912
1913 for (i = 0; i < 512; ++i) {
1914 if (!(tbl[i] & GCR3_VALID))
1915 continue;
1916
1917 ptr = __va(tbl[i] & PAGE_MASK);
1918
1919 free_gcr3_tbl_level1(ptr);
1920 }
1921}
1922
Joerg Roedel52815b72011-11-17 17:24:28 +01001923static void free_gcr3_table(struct protection_domain *domain)
1924{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001925 if (domain->glx == 2)
1926 free_gcr3_tbl_level2(domain->gcr3_tbl);
1927 else if (domain->glx == 1)
1928 free_gcr3_tbl_level1(domain->gcr3_tbl);
Joerg Roedel23d3a982015-08-13 11:15:13 +02001929 else
1930 BUG_ON(domain->glx != 0);
Joerg Roedelb16137b2011-11-21 16:50:23 +01001931
Joerg Roedel52815b72011-11-17 17:24:28 +01001932 free_page((unsigned long)domain->gcr3_tbl);
1933}
1934
Joerg Roedel431b2a22008-07-11 17:14:22 +02001935/*
1936 * Free a domain, only used if something went wrong in the
1937 * allocation path and we need to free an already allocated page table
1938 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001939static void dma_ops_domain_free(struct dma_ops_domain *dom)
1940{
Joerg Roedel384de722009-05-15 12:30:05 +02001941 int i;
1942
Joerg Roedelec487d12008-06-26 21:27:58 +02001943 if (!dom)
1944 return;
1945
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001946 free_percpu(dom->next_index);
1947
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001948 del_domain_from_list(&dom->domain);
1949
Joerg Roedel86db2e52008-12-02 18:20:21 +01001950 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001951
Joerg Roedel384de722009-05-15 12:30:05 +02001952 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1953 if (!dom->aperture[i])
1954 continue;
1955 free_page((unsigned long)dom->aperture[i]->bitmap);
1956 kfree(dom->aperture[i]);
1957 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001958
1959 kfree(dom);
1960}
1961
Joerg Roedela639a8e2015-12-22 16:06:49 +01001962static int dma_ops_domain_alloc_apertures(struct dma_ops_domain *dma_dom,
1963 int max_apertures)
1964{
1965 int ret, i, apertures;
1966
1967 apertures = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
1968 ret = 0;
1969
1970 for (i = apertures; i < max_apertures; ++i) {
1971 ret = alloc_new_range(dma_dom, false, GFP_KERNEL);
1972 if (ret)
1973 break;
1974 }
1975
1976 return ret;
1977}
1978
Joerg Roedel431b2a22008-07-11 17:14:22 +02001979/*
1980 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001981 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001982 * structures required for the dma_ops interface
1983 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001984static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001985{
1986 struct dma_ops_domain *dma_dom;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001987 int cpu;
Joerg Roedelec487d12008-06-26 21:27:58 +02001988
1989 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1990 if (!dma_dom)
1991 return NULL;
1992
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001993 if (protection_domain_init(&dma_dom->domain))
Joerg Roedelec487d12008-06-26 21:27:58 +02001994 goto free_dma_dom;
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001995
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001996 dma_dom->next_index = alloc_percpu(u32);
1997 if (!dma_dom->next_index)
1998 goto free_dma_dom;
1999
Joerg Roedel8f7a0172009-09-02 16:55:24 +02002000 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02002001 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01002002 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02002003 dma_dom->domain.priv = dma_dom;
2004 if (!dma_dom->domain.pt_root)
2005 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002006
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002007 add_domain_to_list(&dma_dom->domain);
2008
Joerg Roedel576175c2009-11-23 19:08:46 +01002009 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02002010 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002011
Joerg Roedel431b2a22008-07-11 17:14:22 +02002012 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02002013 * mark the first page as allocated so we never return 0 as
2014 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02002015 */
Joerg Roedel384de722009-05-15 12:30:05 +02002016 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedelec487d12008-06-26 21:27:58 +02002017
Joerg Roedel5f6bed52015-12-22 13:34:22 +01002018 for_each_possible_cpu(cpu)
2019 *per_cpu_ptr(dma_dom->next_index, cpu) = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02002020
2021 return dma_dom;
2022
2023free_dma_dom:
2024 dma_ops_domain_free(dma_dom);
2025
2026 return NULL;
2027}
2028
Joerg Roedel431b2a22008-07-11 17:14:22 +02002029/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01002030 * little helper function to check whether a given protection domain is a
2031 * dma_ops domain
2032 */
2033static bool dma_ops_domain(struct protection_domain *domain)
2034{
2035 return domain->flags & PD_DMA_OPS_MASK;
2036}
2037
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002038static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002039{
Joerg Roedel132bd682011-11-17 14:18:46 +01002040 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01002041 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01002042
Joerg Roedel132bd682011-11-17 14:18:46 +01002043 if (domain->mode != PAGE_MODE_NONE)
2044 pte_root = virt_to_phys(domain->pt_root);
2045
Joerg Roedel38ddf412008-09-11 10:38:32 +02002046 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
2047 << DEV_ENTRY_MODE_SHIFT;
2048 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002049
Joerg Roedelee6c2862011-11-09 12:06:03 +01002050 flags = amd_iommu_dev_table[devid].data[1];
2051
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002052 if (ats)
2053 flags |= DTE_FLAG_IOTLB;
2054
Joerg Roedel52815b72011-11-17 17:24:28 +01002055 if (domain->flags & PD_IOMMUV2_MASK) {
2056 u64 gcr3 = __pa(domain->gcr3_tbl);
2057 u64 glx = domain->glx;
2058 u64 tmp;
2059
2060 pte_root |= DTE_FLAG_GV;
2061 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
2062
2063 /* First mask out possible old values for GCR3 table */
2064 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
2065 flags &= ~tmp;
2066
2067 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
2068 flags &= ~tmp;
2069
2070 /* Encode GCR3 table into DTE */
2071 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
2072 pte_root |= tmp;
2073
2074 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
2075 flags |= tmp;
2076
2077 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
2078 flags |= tmp;
2079 }
2080
Joerg Roedelee6c2862011-11-09 12:06:03 +01002081 flags &= ~(0xffffUL);
2082 flags |= domain->id;
2083
2084 amd_iommu_dev_table[devid].data[1] = flags;
2085 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002086}
2087
Joerg Roedel15898bb2009-11-24 15:39:42 +01002088static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002089{
Joerg Roedel355bf552008-12-08 12:02:41 +01002090 /* remove entry from the device table seen by the hardware */
Joerg Roedelcbf3ccd2015-10-20 14:59:36 +02002091 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2092 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
Joerg Roedel355bf552008-12-08 12:02:41 +01002093
Joerg Roedelc5cca142009-10-09 18:31:20 +02002094 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002095}
2096
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002097static void do_attach(struct iommu_dev_data *dev_data,
2098 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002099{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002100 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02002101 u16 alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002102 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002103
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002104 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele25bfb52015-10-20 17:33:38 +02002105 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002106 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002107
2108 /* Update data structures */
2109 dev_data->domain = domain;
2110 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002111
2112 /* Do reference counting */
2113 domain->dev_iommu[iommu->index] += 1;
2114 domain->dev_cnt += 1;
2115
Joerg Roedele25bfb52015-10-20 17:33:38 +02002116 /* Update device table */
2117 set_dte_entry(dev_data->devid, domain, ats);
2118 if (alias != dev_data->devid)
Baoquan He9b1a12d2016-01-20 22:01:19 +08002119 set_dte_entry(alias, domain, ats);
Joerg Roedele25bfb52015-10-20 17:33:38 +02002120
Joerg Roedel6c542042011-06-09 17:07:31 +02002121 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002122}
2123
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002124static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002125{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002126 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02002127 u16 alias;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002128
Joerg Roedel5adad992015-10-09 16:23:33 +02002129 /*
2130 * First check if the device is still attached. It might already
2131 * be detached from its domain because the generic
2132 * iommu_detach_group code detached it and we try again here in
2133 * our alias handling.
2134 */
2135 if (!dev_data->domain)
2136 return;
2137
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002138 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele25bfb52015-10-20 17:33:38 +02002139 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002140
Joerg Roedelc4596112009-11-20 14:57:32 +01002141 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002142 dev_data->domain->dev_iommu[iommu->index] -= 1;
2143 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002144
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002145 /* Update data structures */
2146 dev_data->domain = NULL;
2147 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002148 clear_dte_entry(dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02002149 if (alias != dev_data->devid)
2150 clear_dte_entry(alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002151
2152 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002153 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002154}
2155
2156/*
2157 * If a device is not yet associated with a domain, this function does
2158 * assigns it visible for the hardware
2159 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002160static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002161 struct protection_domain *domain)
2162{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002163 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002164
Joerg Roedel272e4f92015-10-20 17:33:37 +02002165 /*
2166 * Must be called with IRQs disabled. Warn here to detect early
2167 * when its not.
2168 */
2169 WARN_ON(!irqs_disabled());
2170
Joerg Roedel15898bb2009-11-24 15:39:42 +01002171 /* lock domain */
2172 spin_lock(&domain->lock);
2173
Joerg Roedel397111a2014-08-05 17:31:51 +02002174 ret = -EBUSY;
Joerg Roedel150952f2015-10-20 17:33:35 +02002175 if (dev_data->domain != NULL)
Joerg Roedel397111a2014-08-05 17:31:51 +02002176 goto out_unlock;
Joerg Roedel24100052009-11-25 15:59:57 +01002177
Joerg Roedel397111a2014-08-05 17:31:51 +02002178 /* Attach alias group root */
Joerg Roedel150952f2015-10-20 17:33:35 +02002179 do_attach(dev_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002180
Julia Lawall84fe6c12010-05-27 12:31:51 +02002181 ret = 0;
2182
2183out_unlock:
2184
Joerg Roedel355bf552008-12-08 12:02:41 +01002185 /* ready */
2186 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002187
Julia Lawall84fe6c12010-05-27 12:31:51 +02002188 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002189}
2190
Joerg Roedel52815b72011-11-17 17:24:28 +01002191
2192static void pdev_iommuv2_disable(struct pci_dev *pdev)
2193{
2194 pci_disable_ats(pdev);
2195 pci_disable_pri(pdev);
2196 pci_disable_pasid(pdev);
2197}
2198
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002199/* FIXME: Change generic reset-function to do the same */
2200static int pri_reset_while_enabled(struct pci_dev *pdev)
2201{
2202 u16 control;
2203 int pos;
2204
Joerg Roedel46277b72011-12-07 14:34:02 +01002205 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002206 if (!pos)
2207 return -EINVAL;
2208
Joerg Roedel46277b72011-12-07 14:34:02 +01002209 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2210 control |= PCI_PRI_CTRL_RESET;
2211 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002212
2213 return 0;
2214}
2215
Joerg Roedel52815b72011-11-17 17:24:28 +01002216static int pdev_iommuv2_enable(struct pci_dev *pdev)
2217{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002218 bool reset_enable;
2219 int reqs, ret;
2220
2221 /* FIXME: Hardcode number of outstanding requests for now */
2222 reqs = 32;
2223 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2224 reqs = 1;
2225 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002226
2227 /* Only allow access to user-accessible pages */
2228 ret = pci_enable_pasid(pdev, 0);
2229 if (ret)
2230 goto out_err;
2231
2232 /* First reset the PRI state of the device */
2233 ret = pci_reset_pri(pdev);
2234 if (ret)
2235 goto out_err;
2236
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002237 /* Enable PRI */
2238 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002239 if (ret)
2240 goto out_err;
2241
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002242 if (reset_enable) {
2243 ret = pri_reset_while_enabled(pdev);
2244 if (ret)
2245 goto out_err;
2246 }
2247
Joerg Roedel52815b72011-11-17 17:24:28 +01002248 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2249 if (ret)
2250 goto out_err;
2251
2252 return 0;
2253
2254out_err:
2255 pci_disable_pri(pdev);
2256 pci_disable_pasid(pdev);
2257
2258 return ret;
2259}
2260
Joerg Roedelc99afa22011-11-21 18:19:25 +01002261/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002262#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002263
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002264static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002265{
Joerg Roedela3b93122012-04-12 12:49:26 +02002266 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002267 int pos;
2268
Joerg Roedel46277b72011-12-07 14:34:02 +01002269 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002270 if (!pos)
2271 return false;
2272
Joerg Roedela3b93122012-04-12 12:49:26 +02002273 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002274
Joerg Roedela3b93122012-04-12 12:49:26 +02002275 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002276}
2277
Joerg Roedel15898bb2009-11-24 15:39:42 +01002278/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002279 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002280 * assigns it visible for the hardware
2281 */
2282static int attach_device(struct device *dev,
2283 struct protection_domain *domain)
2284{
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002285 struct pci_dev *pdev;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002286 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002287 unsigned long flags;
2288 int ret;
2289
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002290 dev_data = get_dev_data(dev);
2291
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002292 if (!dev_is_pci(dev))
2293 goto skip_ats_check;
2294
2295 pdev = to_pci_dev(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002296 if (domain->flags & PD_IOMMUV2_MASK) {
Joerg Roedel02ca2022015-07-28 16:58:49 +02002297 if (!dev_data->passthrough)
Joerg Roedel52815b72011-11-17 17:24:28 +01002298 return -EINVAL;
2299
Joerg Roedel02ca2022015-07-28 16:58:49 +02002300 if (dev_data->iommu_v2) {
2301 if (pdev_iommuv2_enable(pdev) != 0)
2302 return -EINVAL;
Joerg Roedel52815b72011-11-17 17:24:28 +01002303
Joerg Roedel02ca2022015-07-28 16:58:49 +02002304 dev_data->ats.enabled = true;
2305 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2306 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
2307 }
Joerg Roedel52815b72011-11-17 17:24:28 +01002308 } else if (amd_iommu_iotlb_sup &&
2309 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002310 dev_data->ats.enabled = true;
2311 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2312 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002313
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002314skip_ats_check:
Joerg Roedel15898bb2009-11-24 15:39:42 +01002315 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002316 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002317 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2318
2319 /*
2320 * We might boot into a crash-kernel here. The crashed kernel
2321 * left the caches in the IOMMU dirty. So we have to flush
2322 * here to evict all dirty stuff.
2323 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002324 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002325
2326 return ret;
2327}
2328
2329/*
2330 * Removes a device from a protection domain (unlocked)
2331 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002332static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002333{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002334 struct protection_domain *domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002335
Joerg Roedel272e4f92015-10-20 17:33:37 +02002336 /*
2337 * Must be called with IRQs disabled. Warn here to detect early
2338 * when its not.
2339 */
2340 WARN_ON(!irqs_disabled());
2341
Joerg Roedelf34c73f2015-10-20 17:33:34 +02002342 if (WARN_ON(!dev_data->domain))
2343 return;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002344
Joerg Roedel2ca76272010-01-22 16:45:31 +01002345 domain = dev_data->domain;
2346
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002347 spin_lock(&domain->lock);
Joerg Roedel24100052009-11-25 15:59:57 +01002348
Joerg Roedel150952f2015-10-20 17:33:35 +02002349 do_detach(dev_data);
Joerg Roedel71f77582011-06-09 19:03:15 +02002350
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002351 spin_unlock(&domain->lock);
Joerg Roedel355bf552008-12-08 12:02:41 +01002352}
2353
2354/*
2355 * Removes a device from a protection domain (with devtable_lock held)
2356 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002357static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002358{
Joerg Roedel52815b72011-11-17 17:24:28 +01002359 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002360 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002361 unsigned long flags;
2362
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002363 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002364 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002365
Joerg Roedel355bf552008-12-08 12:02:41 +01002366 /* lock device table */
2367 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002368 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002369 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002370
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002371 if (!dev_is_pci(dev))
2372 return;
2373
Joerg Roedel02ca2022015-07-28 16:58:49 +02002374 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
Joerg Roedel52815b72011-11-17 17:24:28 +01002375 pdev_iommuv2_disable(to_pci_dev(dev));
2376 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002377 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002378
2379 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002380}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002381
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002382static int amd_iommu_add_device(struct device *dev)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002383{
Joerg Roedel71f77582011-06-09 19:03:15 +02002384 struct iommu_dev_data *dev_data;
Joerg Roedel07ee8692015-05-28 18:41:42 +02002385 struct iommu_domain *domain;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002386 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002387 int ret, devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002388
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002389 if (!check_device(dev) || get_dev_data(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002390 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002391
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002392 devid = get_device_id(dev);
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002393 if (IS_ERR_VALUE(devid))
2394 return devid;
2395
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002396 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01002397
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002398 ret = iommu_init_device(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002399 if (ret) {
2400 if (ret != -ENOTSUPP)
2401 pr_err("Failed to initialize device %s - trying to proceed anyway\n",
2402 dev_name(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +01002403
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002404 iommu_ignore_device(dev);
Joerg Roedel343e9ca2015-05-28 18:41:43 +02002405 dev->archdata.dma_ops = &nommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002406 goto out;
2407 }
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002408 init_iommu_group(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002409
Joerg Roedel07ee8692015-05-28 18:41:42 +02002410 dev_data = get_dev_data(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002411
2412 BUG_ON(!dev_data);
2413
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002414 if (iommu_pass_through || dev_data->iommu_v2)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002415 iommu_request_dm_for_dev(dev);
2416
2417 /* Domains are initialized for this device - have a look what we ended up with */
2418 domain = iommu_get_domain_for_dev(dev);
Joerg Roedel32302322015-07-28 16:58:50 +02002419 if (domain->type == IOMMU_DOMAIN_IDENTITY)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002420 dev_data->passthrough = true;
Joerg Roedel32302322015-07-28 16:58:50 +02002421 else
Joerg Roedel07ee8692015-05-28 18:41:42 +02002422 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002423
2424out:
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002425 iommu_completion_wait(iommu);
2426
Joerg Roedele275a2a2008-12-10 18:27:25 +01002427 return 0;
2428}
2429
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002430static void amd_iommu_remove_device(struct device *dev)
Joerg Roedel8638c492009-12-10 11:12:25 +01002431{
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002432 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002433 int devid;
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002434
2435 if (!check_device(dev))
2436 return;
2437
2438 devid = get_device_id(dev);
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002439 if (IS_ERR_VALUE(devid))
2440 return;
2441
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002442 iommu = amd_iommu_rlookup_table[devid];
2443
2444 iommu_uninit_device(dev);
2445 iommu_completion_wait(iommu);
Joerg Roedel8638c492009-12-10 11:12:25 +01002446}
2447
Joerg Roedel431b2a22008-07-11 17:14:22 +02002448/*****************************************************************************
2449 *
2450 * The next functions belong to the dma_ops mapping/unmapping code.
2451 *
2452 *****************************************************************************/
2453
2454/*
2455 * In the dma_ops path we only have the struct device. This function
2456 * finds the corresponding IOMMU, the protection domain and the
2457 * requestor id for a given device.
2458 * If the device is not yet associated with a domain this is also done
2459 * in this function.
2460 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002461static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002462{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002463 struct protection_domain *domain;
Joerg Roedel063071d2015-05-28 18:41:38 +02002464 struct iommu_domain *io_domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002465
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002466 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002467 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002468
Joerg Roedel063071d2015-05-28 18:41:38 +02002469 io_domain = iommu_get_domain_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002470 if (!io_domain)
2471 return NULL;
Joerg Roedel063071d2015-05-28 18:41:38 +02002472
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002473 domain = to_pdomain(io_domain);
2474 if (!dma_ops_domain(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002475 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002476
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002477 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002478}
2479
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002480static void update_device_table(struct protection_domain *domain)
2481{
Joerg Roedel492667d2009-11-27 13:25:47 +01002482 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002483
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002484 list_for_each_entry(dev_data, &domain->dev_list, list)
2485 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002486}
2487
2488static void update_domain(struct protection_domain *domain)
2489{
2490 if (!domain->updated)
2491 return;
2492
2493 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002494
2495 domain_flush_devices(domain);
2496 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002497
2498 domain->updated = false;
2499}
2500
Joerg Roedel431b2a22008-07-11 17:14:22 +02002501/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002502 * This function fetches the PTE for a given address in the aperture
2503 */
2504static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2505 unsigned long address)
2506{
Joerg Roedel384de722009-05-15 12:30:05 +02002507 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002508 u64 *pte, *pte_page;
2509
Joerg Roedel384de722009-05-15 12:30:05 +02002510 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2511 if (!aperture)
2512 return NULL;
2513
2514 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002515 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002516 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002517 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002518 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2519 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002520 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002521
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002522 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002523
2524 return pte;
2525}
2526
2527/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002528 * This is the generic map function. It maps one 4kb page at paddr to
2529 * the given address in the DMA address space for the domain.
2530 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002531static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002532 unsigned long address,
2533 phys_addr_t paddr,
2534 int direction)
2535{
2536 u64 *pte, __pte;
2537
2538 WARN_ON(address > dom->aperture_size);
2539
2540 paddr &= PAGE_MASK;
2541
Joerg Roedel8bda3092009-05-12 12:02:46 +02002542 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002543 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002544 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002545
2546 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2547
2548 if (direction == DMA_TO_DEVICE)
2549 __pte |= IOMMU_PTE_IR;
2550 else if (direction == DMA_FROM_DEVICE)
2551 __pte |= IOMMU_PTE_IW;
2552 else if (direction == DMA_BIDIRECTIONAL)
2553 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2554
Joerg Roedela7fb6682015-12-21 12:50:54 +01002555 WARN_ON_ONCE(*pte);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002556
2557 *pte = __pte;
2558
2559 return (dma_addr_t)address;
2560}
2561
Joerg Roedel431b2a22008-07-11 17:14:22 +02002562/*
2563 * The generic unmapping function for on page in the DMA address space.
2564 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002565static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002566 unsigned long address)
2567{
Joerg Roedel384de722009-05-15 12:30:05 +02002568 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002569 u64 *pte;
2570
2571 if (address >= dom->aperture_size)
2572 return;
2573
Joerg Roedel384de722009-05-15 12:30:05 +02002574 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2575 if (!aperture)
2576 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002577
Joerg Roedel384de722009-05-15 12:30:05 +02002578 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2579 if (!pte)
2580 return;
2581
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002582 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002583
Joerg Roedela7fb6682015-12-21 12:50:54 +01002584 WARN_ON_ONCE(!*pte);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002585
2586 *pte = 0ULL;
2587}
2588
Joerg Roedel431b2a22008-07-11 17:14:22 +02002589/*
2590 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002591 * contiguous memory region into DMA address space. It is used by all
2592 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002593 * Must be called with the domain lock held.
2594 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002595static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002596 struct dma_ops_domain *dma_dom,
2597 phys_addr_t paddr,
2598 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002599 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002600 bool align,
2601 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002602{
2603 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002604 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002605 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002606 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002607 int i;
2608
Joerg Roedele3c449f2008-10-15 22:02:11 -07002609 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002610 paddr &= PAGE_MASK;
2611
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002612 INC_STATS_COUNTER(total_map_requests);
2613
Joerg Roedelc1858972008-12-12 15:42:39 +01002614 if (pages > 1)
2615 INC_STATS_COUNTER(cross_page);
2616
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002617 if (align)
2618 align_mask = (1UL << get_order(size)) - 1;
2619
Joerg Roedel832a90c2008-09-18 15:54:23 +02002620 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2621 dma_mask);
Joerg Roedelebaecb42015-12-21 18:11:32 +01002622
Joerg Roedel266a3bd2015-12-21 18:54:24 +01002623 if (address == DMA_ERROR_CODE)
2624 goto out;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002625
2626 start = address;
2627 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002628 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002629 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002630 goto out_unmap;
2631
Joerg Roedelcb76c322008-06-26 21:28:00 +02002632 paddr += PAGE_SIZE;
2633 start += PAGE_SIZE;
2634 }
2635 address += offset;
2636
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002637 ADD_STATS_COUNTER(alloced_io_mem, size);
2638
Joerg Roedelab7032b2015-12-21 18:47:11 +01002639 if (unlikely(amd_iommu_np_cache)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002640 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedelab7032b2015-12-21 18:47:11 +01002641 domain_flush_complete(&dma_dom->domain);
2642 }
Joerg Roedel270cab242008-09-04 15:49:46 +02002643
Joerg Roedelcb76c322008-06-26 21:28:00 +02002644out:
2645 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002646
2647out_unmap:
2648
2649 for (--i; i >= 0; --i) {
2650 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002651 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002652 }
2653
2654 dma_ops_free_addresses(dma_dom, address, pages);
2655
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002656 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002657}
2658
Joerg Roedel431b2a22008-07-11 17:14:22 +02002659/*
2660 * Does the reverse of the __map_single function. Must be called with
2661 * the domain lock held too
2662 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002663static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002664 dma_addr_t dma_addr,
2665 size_t size,
2666 int dir)
2667{
Joerg Roedel04e04632010-09-23 16:12:48 +02002668 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002669 dma_addr_t i, start;
2670 unsigned int pages;
2671
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002672 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002673 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002674 return;
2675
Joerg Roedel04e04632010-09-23 16:12:48 +02002676 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002677 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002678 dma_addr &= PAGE_MASK;
2679 start = dma_addr;
2680
2681 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002682 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002683 start += PAGE_SIZE;
2684 }
2685
Joerg Roedel84b3a0b2015-12-21 13:23:59 +01002686 SUB_STATS_COUNTER(alloced_io_mem, size);
2687
2688 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002689}
2690
Joerg Roedel431b2a22008-07-11 17:14:22 +02002691/*
2692 * The exported map_single function for dma_ops.
2693 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002694static dma_addr_t map_page(struct device *dev, struct page *page,
2695 unsigned long offset, size_t size,
2696 enum dma_data_direction dir,
2697 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002698{
FUJITA Tomonori51491362009-01-05 23:47:25 +09002699 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002700 struct protection_domain *domain;
2701 u64 dma_mask;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002702
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002703 INC_STATS_COUNTER(cnt_map_single);
2704
Joerg Roedel94f6d192009-11-24 16:40:02 +01002705 domain = get_domain(dev);
2706 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002707 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002708 else if (IS_ERR(domain))
2709 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002710
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002711 dma_mask = *dev->dma_mask;
2712
Joerg Roedel92d420e2015-12-21 19:31:33 +01002713 return __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002714 dma_mask);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002715}
2716
Joerg Roedel431b2a22008-07-11 17:14:22 +02002717/*
2718 * The exported unmap_single function for dma_ops.
2719 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002720static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2721 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002722{
Joerg Roedel4da70b92008-06-26 21:28:01 +02002723 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002724
Joerg Roedel146a6912008-12-12 15:07:12 +01002725 INC_STATS_COUNTER(cnt_unmap_single);
2726
Joerg Roedel94f6d192009-11-24 16:40:02 +01002727 domain = get_domain(dev);
2728 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002729 return;
2730
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002731 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002732}
2733
Joerg Roedel431b2a22008-07-11 17:14:22 +02002734/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002735 * The exported map_sg function for dma_ops (handles scatter-gather
2736 * lists).
2737 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002738static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002739 int nelems, enum dma_data_direction dir,
2740 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002741{
Joerg Roedel65b050a2008-06-26 21:28:02 +02002742 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002743 int i;
2744 struct scatterlist *s;
2745 phys_addr_t paddr;
2746 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002747 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002748
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002749 INC_STATS_COUNTER(cnt_map_sg);
2750
Joerg Roedel94f6d192009-11-24 16:40:02 +01002751 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002752 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002753 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002754
Joerg Roedel832a90c2008-09-18 15:54:23 +02002755 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002756
Joerg Roedel65b050a2008-06-26 21:28:02 +02002757 for_each_sg(sglist, s, nelems, i) {
2758 paddr = sg_phys(s);
2759
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002760 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002761 paddr, s->length, dir, false,
2762 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002763
2764 if (s->dma_address) {
2765 s->dma_length = s->length;
2766 mapped_elems++;
2767 } else
2768 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002769 }
2770
Joerg Roedel65b050a2008-06-26 21:28:02 +02002771 return mapped_elems;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002772
Joerg Roedel65b050a2008-06-26 21:28:02 +02002773unmap:
2774 for_each_sg(sglist, s, mapped_elems, i) {
2775 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002776 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002777 s->dma_length, dir);
2778 s->dma_address = s->dma_length = 0;
2779 }
2780
Joerg Roedel92d420e2015-12-21 19:31:33 +01002781 return 0;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002782}
2783
Joerg Roedel431b2a22008-07-11 17:14:22 +02002784/*
2785 * The exported map_sg function for dma_ops (handles scatter-gather
2786 * lists).
2787 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002788static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002789 int nelems, enum dma_data_direction dir,
2790 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002791{
Joerg Roedel65b050a2008-06-26 21:28:02 +02002792 struct protection_domain *domain;
2793 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002794 int i;
2795
Joerg Roedel55877a62008-12-12 15:12:14 +01002796 INC_STATS_COUNTER(cnt_unmap_sg);
2797
Joerg Roedel94f6d192009-11-24 16:40:02 +01002798 domain = get_domain(dev);
2799 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002800 return;
2801
Joerg Roedel65b050a2008-06-26 21:28:02 +02002802 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002803 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002804 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002805 s->dma_address = s->dma_length = 0;
2806 }
Joerg Roedel65b050a2008-06-26 21:28:02 +02002807}
2808
Joerg Roedel431b2a22008-07-11 17:14:22 +02002809/*
2810 * The exported alloc_coherent function for dma_ops.
2811 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002812static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002813 dma_addr_t *dma_addr, gfp_t flag,
2814 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002815{
Joerg Roedel832a90c2008-09-18 15:54:23 +02002816 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002817 struct protection_domain *domain;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002818 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002819
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002820 INC_STATS_COUNTER(cnt_alloc_coherent);
2821
Joerg Roedel94f6d192009-11-24 16:40:02 +01002822 domain = get_domain(dev);
2823 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedel3b839a52015-04-01 14:58:47 +02002824 page = alloc_pages(flag, get_order(size));
2825 *dma_addr = page_to_phys(page);
2826 return page_address(page);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002827 } else if (IS_ERR(domain))
2828 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002829
Joerg Roedel3b839a52015-04-01 14:58:47 +02002830 size = PAGE_ALIGN(size);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002831 dma_mask = dev->coherent_dma_mask;
2832 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
Joerg Roedel2d0ec7a2015-06-01 17:30:57 +02002833 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002834
Joerg Roedel3b839a52015-04-01 14:58:47 +02002835 page = alloc_pages(flag | __GFP_NOWARN, get_order(size));
2836 if (!page) {
Mel Gormand0164ad2015-11-06 16:28:21 -08002837 if (!gfpflags_allow_blocking(flag))
Joerg Roedel3b839a52015-04-01 14:58:47 +02002838 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002839
Joerg Roedel3b839a52015-04-01 14:58:47 +02002840 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
2841 get_order(size));
2842 if (!page)
2843 return NULL;
2844 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002845
Joerg Roedel832a90c2008-09-18 15:54:23 +02002846 if (!dma_mask)
2847 dma_mask = *dev->dma_mask;
2848
Joerg Roedel3b839a52015-04-01 14:58:47 +02002849 *dma_addr = __map_single(dev, domain->priv, page_to_phys(page),
Joerg Roedel832a90c2008-09-18 15:54:23 +02002850 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002851
Joerg Roedel92d420e2015-12-21 19:31:33 +01002852 if (*dma_addr == DMA_ERROR_CODE)
Joerg Roedel5b28df62008-12-02 17:49:42 +01002853 goto out_free;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002854
Joerg Roedel3b839a52015-04-01 14:58:47 +02002855 return page_address(page);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002856
2857out_free:
2858
Joerg Roedel3b839a52015-04-01 14:58:47 +02002859 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2860 __free_pages(page, get_order(size));
Joerg Roedel5b28df62008-12-02 17:49:42 +01002861
2862 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002863}
2864
Joerg Roedel431b2a22008-07-11 17:14:22 +02002865/*
2866 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002867 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002868static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002869 void *virt_addr, dma_addr_t dma_addr,
2870 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002871{
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002872 struct protection_domain *domain;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002873 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002874
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002875 INC_STATS_COUNTER(cnt_free_coherent);
2876
Joerg Roedel3b839a52015-04-01 14:58:47 +02002877 page = virt_to_page(virt_addr);
2878 size = PAGE_ALIGN(size);
2879
Joerg Roedel94f6d192009-11-24 16:40:02 +01002880 domain = get_domain(dev);
2881 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002882 goto free_mem;
2883
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002884 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002885
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002886free_mem:
Joerg Roedel3b839a52015-04-01 14:58:47 +02002887 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2888 __free_pages(page, get_order(size));
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002889}
2890
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002891/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002892 * This function is called by the DMA layer to find out if we can handle a
2893 * particular device. It is part of the dma_ops.
2894 */
2895static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2896{
Joerg Roedel420aef82009-11-23 16:14:57 +01002897 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002898}
2899
Joerg Roedela639a8e2015-12-22 16:06:49 +01002900static int set_dma_mask(struct device *dev, u64 mask)
2901{
2902 struct protection_domain *domain;
2903 int max_apertures = 1;
2904
2905 domain = get_domain(dev);
2906 if (IS_ERR(domain))
2907 return PTR_ERR(domain);
2908
2909 if (mask == DMA_BIT_MASK(64))
2910 max_apertures = 8;
2911 else if (mask > DMA_BIT_MASK(32))
2912 max_apertures = 4;
2913
2914 /*
2915 * To prevent lock contention it doesn't make sense to allocate more
2916 * apertures than online cpus
2917 */
2918 if (max_apertures > num_online_cpus())
2919 max_apertures = num_online_cpus();
2920
2921 if (dma_ops_domain_alloc_apertures(domain->priv, max_apertures))
2922 dev_err(dev, "Can't allocate %d iommu apertures\n",
2923 max_apertures);
2924
2925 return 0;
2926}
2927
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002928static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedela639a8e2015-12-22 16:06:49 +01002929 .alloc = alloc_coherent,
2930 .free = free_coherent,
2931 .map_page = map_page,
2932 .unmap_page = unmap_page,
2933 .map_sg = map_sg,
2934 .unmap_sg = unmap_sg,
2935 .dma_supported = amd_iommu_dma_supported,
2936 .set_dma_mask = set_dma_mask,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002937};
2938
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002939int __init amd_iommu_init_api(void)
Joerg Roedel27c21272011-05-30 15:56:24 +02002940{
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002941 return bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01002942}
2943
Joerg Roedel6631ee92008-06-26 21:28:05 +02002944int __init amd_iommu_init_dma_ops(void)
2945{
Joerg Roedel32302322015-07-28 16:58:50 +02002946 swiotlb = iommu_pass_through ? 1 : 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002947 iommu_detected = 1;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002948
Joerg Roedel52717822015-07-28 16:58:51 +02002949 /*
2950 * In case we don't initialize SWIOTLB (actually the common case
2951 * when AMD IOMMU is enabled), make sure there are global
2952 * dma_ops set as a fall-back for devices not handled by this
2953 * driver (for example non-PCI devices).
2954 */
2955 if (!swiotlb)
2956 dma_ops = &nommu_dma_ops;
2957
Joerg Roedel7f265082008-12-12 13:50:21 +01002958 amd_iommu_stats_init();
2959
Joerg Roedel62410ee2012-06-12 16:42:43 +02002960 if (amd_iommu_unmap_flush)
2961 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
2962 else
2963 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
2964
Joerg Roedel6631ee92008-06-26 21:28:05 +02002965 return 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002966}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002967
2968/*****************************************************************************
2969 *
2970 * The following functions belong to the exported interface of AMD IOMMU
2971 *
2972 * This interface allows access to lower level functions of the IOMMU
2973 * like protection domain handling and assignement of devices to domains
2974 * which is not possible with the dma_ops interface.
2975 *
2976 *****************************************************************************/
2977
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002978static void cleanup_domain(struct protection_domain *domain)
2979{
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002980 struct iommu_dev_data *entry;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002981 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002982
2983 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2984
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002985 while (!list_empty(&domain->dev_list)) {
2986 entry = list_first_entry(&domain->dev_list,
2987 struct iommu_dev_data, list);
2988 __detach_device(entry);
Joerg Roedel492667d2009-11-27 13:25:47 +01002989 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002990
2991 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2992}
2993
Joerg Roedel26508152009-08-26 16:52:40 +02002994static void protection_domain_free(struct protection_domain *domain)
2995{
2996 if (!domain)
2997 return;
2998
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002999 del_domain_from_list(domain);
3000
Joerg Roedel26508152009-08-26 16:52:40 +02003001 if (domain->id)
3002 domain_id_free(domain->id);
3003
3004 kfree(domain);
3005}
3006
Joerg Roedel7a5a5662015-06-30 08:56:11 +02003007static int protection_domain_init(struct protection_domain *domain)
3008{
3009 spin_lock_init(&domain->lock);
3010 mutex_init(&domain->api_lock);
3011 domain->id = domain_id_alloc();
3012 if (!domain->id)
3013 return -ENOMEM;
3014 INIT_LIST_HEAD(&domain->dev_list);
3015
3016 return 0;
3017}
3018
Joerg Roedel26508152009-08-26 16:52:40 +02003019static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003020{
3021 struct protection_domain *domain;
3022
3023 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3024 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003025 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003026
Joerg Roedel7a5a5662015-06-30 08:56:11 +02003027 if (protection_domain_init(domain))
Joerg Roedel26508152009-08-26 16:52:40 +02003028 goto out_err;
3029
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003030 add_domain_to_list(domain);
3031
Joerg Roedel26508152009-08-26 16:52:40 +02003032 return domain;
3033
3034out_err:
3035 kfree(domain);
3036
3037 return NULL;
3038}
3039
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003040static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
3041{
3042 struct protection_domain *pdomain;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02003043 struct dma_ops_domain *dma_domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003044
Joerg Roedel0bb6e242015-05-28 18:41:40 +02003045 switch (type) {
3046 case IOMMU_DOMAIN_UNMANAGED:
3047 pdomain = protection_domain_alloc();
3048 if (!pdomain)
3049 return NULL;
3050
3051 pdomain->mode = PAGE_MODE_3_LEVEL;
3052 pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3053 if (!pdomain->pt_root) {
3054 protection_domain_free(pdomain);
3055 return NULL;
3056 }
3057
3058 pdomain->domain.geometry.aperture_start = 0;
3059 pdomain->domain.geometry.aperture_end = ~0ULL;
3060 pdomain->domain.geometry.force_aperture = true;
3061
3062 break;
3063 case IOMMU_DOMAIN_DMA:
3064 dma_domain = dma_ops_domain_alloc();
3065 if (!dma_domain) {
3066 pr_err("AMD-Vi: Failed to allocate\n");
3067 return NULL;
3068 }
3069 pdomain = &dma_domain->domain;
3070 break;
Joerg Roedel07f643a2015-05-28 18:41:41 +02003071 case IOMMU_DOMAIN_IDENTITY:
3072 pdomain = protection_domain_alloc();
3073 if (!pdomain)
3074 return NULL;
3075
3076 pdomain->mode = PAGE_MODE_NONE;
3077 break;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02003078 default:
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003079 return NULL;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02003080 }
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003081
3082 return &pdomain->domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003083}
3084
3085static void amd_iommu_domain_free(struct iommu_domain *dom)
Joerg Roedel26508152009-08-26 16:52:40 +02003086{
3087 struct protection_domain *domain;
3088
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003089 if (!dom)
Joerg Roedel98383fc2008-12-02 18:34:12 +01003090 return;
3091
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003092 domain = to_pdomain(dom);
3093
Joerg Roedel98383fc2008-12-02 18:34:12 +01003094 if (domain->dev_cnt > 0)
3095 cleanup_domain(domain);
3096
3097 BUG_ON(domain->dev_cnt != 0);
3098
Joerg Roedel132bd682011-11-17 14:18:46 +01003099 if (domain->mode != PAGE_MODE_NONE)
3100 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003101
Joerg Roedel52815b72011-11-17 17:24:28 +01003102 if (domain->flags & PD_IOMMUV2_MASK)
3103 free_gcr3_table(domain);
3104
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003105 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003106}
3107
Joerg Roedel684f2882008-12-08 12:07:44 +01003108static void amd_iommu_detach_device(struct iommu_domain *dom,
3109 struct device *dev)
3110{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003111 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003112 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003113 int devid;
Joerg Roedel684f2882008-12-08 12:07:44 +01003114
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003115 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003116 return;
3117
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003118 devid = get_device_id(dev);
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003119 if (IS_ERR_VALUE(devid))
3120 return;
Joerg Roedel684f2882008-12-08 12:07:44 +01003121
Joerg Roedel657cbb62009-11-23 15:26:46 +01003122 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003123 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003124
3125 iommu = amd_iommu_rlookup_table[devid];
3126 if (!iommu)
3127 return;
3128
Joerg Roedel684f2882008-12-08 12:07:44 +01003129 iommu_completion_wait(iommu);
3130}
3131
Joerg Roedel01106062008-12-02 19:34:11 +01003132static int amd_iommu_attach_device(struct iommu_domain *dom,
3133 struct device *dev)
3134{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003135 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel657cbb62009-11-23 15:26:46 +01003136 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003137 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003138 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003139
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003140 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003141 return -EINVAL;
3142
Joerg Roedel657cbb62009-11-23 15:26:46 +01003143 dev_data = dev->archdata.iommu;
3144
Joerg Roedelf62dda62011-06-09 12:55:35 +02003145 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003146 if (!iommu)
3147 return -EINVAL;
3148
Joerg Roedel657cbb62009-11-23 15:26:46 +01003149 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003150 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003151
Joerg Roedel15898bb2009-11-24 15:39:42 +01003152 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003153
3154 iommu_completion_wait(iommu);
3155
Joerg Roedel15898bb2009-11-24 15:39:42 +01003156 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003157}
3158
Joerg Roedel468e2362010-01-21 16:37:36 +01003159static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003160 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003161{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003162 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003163 int prot = 0;
3164 int ret;
3165
Joerg Roedel132bd682011-11-17 14:18:46 +01003166 if (domain->mode == PAGE_MODE_NONE)
3167 return -EINVAL;
3168
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003169 if (iommu_prot & IOMMU_READ)
3170 prot |= IOMMU_PROT_IR;
3171 if (iommu_prot & IOMMU_WRITE)
3172 prot |= IOMMU_PROT_IW;
3173
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003174 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003175 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003176 mutex_unlock(&domain->api_lock);
3177
Joerg Roedel795e74f72010-05-11 17:40:57 +02003178 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003179}
3180
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003181static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3182 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003183{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003184 struct protection_domain *domain = to_pdomain(dom);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003185 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003186
Joerg Roedel132bd682011-11-17 14:18:46 +01003187 if (domain->mode == PAGE_MODE_NONE)
3188 return -EINVAL;
3189
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003190 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003191 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003192 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003193
Joerg Roedel17b124b2011-04-06 18:01:35 +02003194 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003195
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003196 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003197}
3198
Joerg Roedel645c4c82008-12-02 20:05:50 +01003199static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547ac2013-03-29 01:23:58 +05303200 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01003201{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003202 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel3039ca12015-04-01 14:58:48 +02003203 unsigned long offset_mask, pte_pgsize;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003204 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003205
Joerg Roedel132bd682011-11-17 14:18:46 +01003206 if (domain->mode == PAGE_MODE_NONE)
3207 return iova;
3208
Joerg Roedel3039ca12015-04-01 14:58:48 +02003209 pte = fetch_pte(domain, iova, &pte_pgsize);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003210
Joerg Roedela6d41a42009-09-02 17:08:55 +02003211 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003212 return 0;
3213
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003214 offset_mask = pte_pgsize - 1;
3215 __pte = *pte & PM_ADDR_MASK;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003216
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003217 return (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003218}
3219
Joerg Roedelab636482014-09-05 10:48:21 +02003220static bool amd_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003221{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003222 switch (cap) {
3223 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedelab636482014-09-05 10:48:21 +02003224 return true;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003225 case IOMMU_CAP_INTR_REMAP:
Joerg Roedelab636482014-09-05 10:48:21 +02003226 return (irq_remapping_enabled == 1);
Will Deaconcfdeec22014-10-27 11:24:48 +00003227 case IOMMU_CAP_NOEXEC:
3228 return false;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003229 }
3230
Joerg Roedelab636482014-09-05 10:48:21 +02003231 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003232}
3233
Joerg Roedel35cf2482015-05-28 18:41:37 +02003234static void amd_iommu_get_dm_regions(struct device *dev,
3235 struct list_head *head)
3236{
3237 struct unity_map_entry *entry;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003238 int devid;
Joerg Roedel35cf2482015-05-28 18:41:37 +02003239
3240 devid = get_device_id(dev);
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003241 if (IS_ERR_VALUE(devid))
3242 return;
Joerg Roedel35cf2482015-05-28 18:41:37 +02003243
3244 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
3245 struct iommu_dm_region *region;
3246
3247 if (devid < entry->devid_start || devid > entry->devid_end)
3248 continue;
3249
3250 region = kzalloc(sizeof(*region), GFP_KERNEL);
3251 if (!region) {
3252 pr_err("Out of memory allocating dm-regions for %s\n",
3253 dev_name(dev));
3254 return;
3255 }
3256
3257 region->start = entry->address_start;
3258 region->length = entry->address_end - entry->address_start;
3259 if (entry->prot & IOMMU_PROT_IR)
3260 region->prot |= IOMMU_READ;
3261 if (entry->prot & IOMMU_PROT_IW)
3262 region->prot |= IOMMU_WRITE;
3263
3264 list_add_tail(&region->list, head);
3265 }
3266}
3267
3268static void amd_iommu_put_dm_regions(struct device *dev,
3269 struct list_head *head)
3270{
3271 struct iommu_dm_region *entry, *next;
3272
3273 list_for_each_entry_safe(entry, next, head, list)
3274 kfree(entry);
3275}
3276
Thierry Redingb22f6432014-06-27 09:03:12 +02003277static const struct iommu_ops amd_iommu_ops = {
Joerg Roedelab636482014-09-05 10:48:21 +02003278 .capable = amd_iommu_capable,
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003279 .domain_alloc = amd_iommu_domain_alloc,
3280 .domain_free = amd_iommu_domain_free,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003281 .attach_dev = amd_iommu_attach_device,
3282 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003283 .map = amd_iommu_map,
3284 .unmap = amd_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07003285 .map_sg = default_iommu_map_sg,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003286 .iova_to_phys = amd_iommu_iova_to_phys,
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02003287 .add_device = amd_iommu_add_device,
3288 .remove_device = amd_iommu_remove_device,
Joerg Roedela960fad2015-10-21 23:51:39 +02003289 .device_group = pci_device_group,
Joerg Roedel35cf2482015-05-28 18:41:37 +02003290 .get_dm_regions = amd_iommu_get_dm_regions,
3291 .put_dm_regions = amd_iommu_put_dm_regions,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003292 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003293};
3294
Joerg Roedel0feae532009-08-26 15:26:30 +02003295/*****************************************************************************
3296 *
3297 * The next functions do a basic initialization of IOMMU for pass through
3298 * mode
3299 *
3300 * In passthrough mode the IOMMU is initialized and enabled but not used for
3301 * DMA-API translation.
3302 *
3303 *****************************************************************************/
3304
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003305/* IOMMUv2 specific functions */
3306int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3307{
3308 return atomic_notifier_chain_register(&ppr_notifier, nb);
3309}
3310EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3311
3312int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3313{
3314 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3315}
3316EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003317
3318void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3319{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003320 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel132bd682011-11-17 14:18:46 +01003321 unsigned long flags;
3322
3323 spin_lock_irqsave(&domain->lock, flags);
3324
3325 /* Update data structure */
3326 domain->mode = PAGE_MODE_NONE;
3327 domain->updated = true;
3328
3329 /* Make changes visible to IOMMUs */
3330 update_domain(domain);
3331
3332 /* Page-table is not visible to IOMMU anymore, so free it */
3333 free_pagetable(domain);
3334
3335 spin_unlock_irqrestore(&domain->lock, flags);
3336}
3337EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003338
3339int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3340{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003341 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel52815b72011-11-17 17:24:28 +01003342 unsigned long flags;
3343 int levels, ret;
3344
3345 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3346 return -EINVAL;
3347
3348 /* Number of GCR3 table levels required */
3349 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3350 levels += 1;
3351
3352 if (levels > amd_iommu_max_glx_val)
3353 return -EINVAL;
3354
3355 spin_lock_irqsave(&domain->lock, flags);
3356
3357 /*
3358 * Save us all sanity checks whether devices already in the
3359 * domain support IOMMUv2. Just force that the domain has no
3360 * devices attached when it is switched into IOMMUv2 mode.
3361 */
3362 ret = -EBUSY;
3363 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3364 goto out;
3365
3366 ret = -ENOMEM;
3367 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3368 if (domain->gcr3_tbl == NULL)
3369 goto out;
3370
3371 domain->glx = levels;
3372 domain->flags |= PD_IOMMUV2_MASK;
3373 domain->updated = true;
3374
3375 update_domain(domain);
3376
3377 ret = 0;
3378
3379out:
3380 spin_unlock_irqrestore(&domain->lock, flags);
3381
3382 return ret;
3383}
3384EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003385
3386static int __flush_pasid(struct protection_domain *domain, int pasid,
3387 u64 address, bool size)
3388{
3389 struct iommu_dev_data *dev_data;
3390 struct iommu_cmd cmd;
3391 int i, ret;
3392
3393 if (!(domain->flags & PD_IOMMUV2_MASK))
3394 return -EINVAL;
3395
3396 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3397
3398 /*
3399 * IOMMU TLB needs to be flushed before Device TLB to
3400 * prevent device TLB refill from IOMMU TLB
3401 */
3402 for (i = 0; i < amd_iommus_present; ++i) {
3403 if (domain->dev_iommu[i] == 0)
3404 continue;
3405
3406 ret = iommu_queue_command(amd_iommus[i], &cmd);
3407 if (ret != 0)
3408 goto out;
3409 }
3410
3411 /* Wait until IOMMU TLB flushes are complete */
3412 domain_flush_complete(domain);
3413
3414 /* Now flush device TLBs */
3415 list_for_each_entry(dev_data, &domain->dev_list, list) {
3416 struct amd_iommu *iommu;
3417 int qdep;
3418
Joerg Roedel1c1cc452015-07-30 11:24:45 +02003419 /*
3420 There might be non-IOMMUv2 capable devices in an IOMMUv2
3421 * domain.
3422 */
3423 if (!dev_data->ats.enabled)
3424 continue;
Joerg Roedel22e266c2011-11-21 15:59:08 +01003425
3426 qdep = dev_data->ats.qdep;
3427 iommu = amd_iommu_rlookup_table[dev_data->devid];
3428
3429 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3430 qdep, address, size);
3431
3432 ret = iommu_queue_command(iommu, &cmd);
3433 if (ret != 0)
3434 goto out;
3435 }
3436
3437 /* Wait until all device TLBs are flushed */
3438 domain_flush_complete(domain);
3439
3440 ret = 0;
3441
3442out:
3443
3444 return ret;
3445}
3446
3447static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3448 u64 address)
3449{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003450 INC_STATS_COUNTER(invalidate_iotlb);
3451
Joerg Roedel22e266c2011-11-21 15:59:08 +01003452 return __flush_pasid(domain, pasid, address, false);
3453}
3454
3455int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3456 u64 address)
3457{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003458 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003459 unsigned long flags;
3460 int ret;
3461
3462 spin_lock_irqsave(&domain->lock, flags);
3463 ret = __amd_iommu_flush_page(domain, pasid, address);
3464 spin_unlock_irqrestore(&domain->lock, flags);
3465
3466 return ret;
3467}
3468EXPORT_SYMBOL(amd_iommu_flush_page);
3469
3470static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3471{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003472 INC_STATS_COUNTER(invalidate_iotlb_all);
3473
Joerg Roedel22e266c2011-11-21 15:59:08 +01003474 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3475 true);
3476}
3477
3478int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3479{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003480 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003481 unsigned long flags;
3482 int ret;
3483
3484 spin_lock_irqsave(&domain->lock, flags);
3485 ret = __amd_iommu_flush_tlb(domain, pasid);
3486 spin_unlock_irqrestore(&domain->lock, flags);
3487
3488 return ret;
3489}
3490EXPORT_SYMBOL(amd_iommu_flush_tlb);
3491
Joerg Roedelb16137b2011-11-21 16:50:23 +01003492static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3493{
3494 int index;
3495 u64 *pte;
3496
3497 while (true) {
3498
3499 index = (pasid >> (9 * level)) & 0x1ff;
3500 pte = &root[index];
3501
3502 if (level == 0)
3503 break;
3504
3505 if (!(*pte & GCR3_VALID)) {
3506 if (!alloc)
3507 return NULL;
3508
3509 root = (void *)get_zeroed_page(GFP_ATOMIC);
3510 if (root == NULL)
3511 return NULL;
3512
3513 *pte = __pa(root) | GCR3_VALID;
3514 }
3515
3516 root = __va(*pte & PAGE_MASK);
3517
3518 level -= 1;
3519 }
3520
3521 return pte;
3522}
3523
3524static int __set_gcr3(struct protection_domain *domain, int pasid,
3525 unsigned long cr3)
3526{
3527 u64 *pte;
3528
3529 if (domain->mode != PAGE_MODE_NONE)
3530 return -EINVAL;
3531
3532 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3533 if (pte == NULL)
3534 return -ENOMEM;
3535
3536 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3537
3538 return __amd_iommu_flush_tlb(domain, pasid);
3539}
3540
3541static int __clear_gcr3(struct protection_domain *domain, int pasid)
3542{
3543 u64 *pte;
3544
3545 if (domain->mode != PAGE_MODE_NONE)
3546 return -EINVAL;
3547
3548 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3549 if (pte == NULL)
3550 return 0;
3551
3552 *pte = 0;
3553
3554 return __amd_iommu_flush_tlb(domain, pasid);
3555}
3556
3557int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3558 unsigned long cr3)
3559{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003560 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003561 unsigned long flags;
3562 int ret;
3563
3564 spin_lock_irqsave(&domain->lock, flags);
3565 ret = __set_gcr3(domain, pasid, cr3);
3566 spin_unlock_irqrestore(&domain->lock, flags);
3567
3568 return ret;
3569}
3570EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3571
3572int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3573{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003574 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003575 unsigned long flags;
3576 int ret;
3577
3578 spin_lock_irqsave(&domain->lock, flags);
3579 ret = __clear_gcr3(domain, pasid);
3580 spin_unlock_irqrestore(&domain->lock, flags);
3581
3582 return ret;
3583}
3584EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003585
3586int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3587 int status, int tag)
3588{
3589 struct iommu_dev_data *dev_data;
3590 struct amd_iommu *iommu;
3591 struct iommu_cmd cmd;
3592
Joerg Roedel399be2f2011-12-01 16:53:47 +01003593 INC_STATS_COUNTER(complete_ppr);
3594
Joerg Roedelc99afa22011-11-21 18:19:25 +01003595 dev_data = get_dev_data(&pdev->dev);
3596 iommu = amd_iommu_rlookup_table[dev_data->devid];
3597
3598 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3599 tag, dev_data->pri_tlp);
3600
3601 return iommu_queue_command(iommu, &cmd);
3602}
3603EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003604
3605struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3606{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003607 struct protection_domain *pdomain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003608
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003609 pdomain = get_domain(&pdev->dev);
3610 if (IS_ERR(pdomain))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003611 return NULL;
3612
3613 /* Only return IOMMUv2 domains */
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003614 if (!(pdomain->flags & PD_IOMMUV2_MASK))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003615 return NULL;
3616
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003617 return &pdomain->domain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003618}
3619EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003620
3621void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3622{
3623 struct iommu_dev_data *dev_data;
3624
3625 if (!amd_iommu_v2_supported())
3626 return;
3627
3628 dev_data = get_dev_data(&pdev->dev);
3629 dev_data->errata |= (1 << erratum);
3630}
3631EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003632
3633int amd_iommu_device_info(struct pci_dev *pdev,
3634 struct amd_iommu_device_info *info)
3635{
3636 int max_pasids;
3637 int pos;
3638
3639 if (pdev == NULL || info == NULL)
3640 return -EINVAL;
3641
3642 if (!amd_iommu_v2_supported())
3643 return -EINVAL;
3644
3645 memset(info, 0, sizeof(*info));
3646
3647 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3648 if (pos)
3649 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3650
3651 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3652 if (pos)
3653 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3654
3655 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3656 if (pos) {
3657 int features;
3658
3659 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3660 max_pasids = min(max_pasids, (1 << 20));
3661
3662 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3663 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3664
3665 features = pci_pasid_features(pdev);
3666 if (features & PCI_PASID_CAP_EXEC)
3667 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3668 if (features & PCI_PASID_CAP_PRIV)
3669 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3670 }
3671
3672 return 0;
3673}
3674EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003675
3676#ifdef CONFIG_IRQ_REMAP
3677
3678/*****************************************************************************
3679 *
3680 * Interrupt Remapping Implementation
3681 *
3682 *****************************************************************************/
3683
3684union irte {
3685 u32 val;
3686 struct {
3687 u32 valid : 1,
3688 no_fault : 1,
3689 int_type : 3,
3690 rq_eoi : 1,
3691 dm : 1,
3692 rsvd_1 : 1,
3693 destination : 8,
3694 vector : 8,
3695 rsvd_2 : 8;
3696 } fields;
3697};
3698
Jiang Liu9c724962015-04-14 10:29:52 +08003699struct irq_2_irte {
3700 u16 devid; /* Device ID for IRTE table */
3701 u16 index; /* Index into IRTE table*/
3702};
3703
Jiang Liu7c71d302015-04-13 14:11:33 +08003704struct amd_ir_data {
3705 struct irq_2_irte irq_2_irte;
3706 union irte irte_entry;
3707 union {
3708 struct msi_msg msi_entry;
3709 };
3710};
3711
3712static struct irq_chip amd_ir_chip;
3713
Joerg Roedel2b324502012-06-21 16:29:10 +02003714#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3715#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3716#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3717#define DTE_IRQ_REMAP_ENABLE 1ULL
3718
3719static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3720{
3721 u64 dte;
3722
3723 dte = amd_iommu_dev_table[devid].data[2];
3724 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3725 dte |= virt_to_phys(table->table);
3726 dte |= DTE_IRQ_REMAP_INTCTL;
3727 dte |= DTE_IRQ_TABLE_LEN;
3728 dte |= DTE_IRQ_REMAP_ENABLE;
3729
3730 amd_iommu_dev_table[devid].data[2] = dte;
3731}
3732
3733#define IRTE_ALLOCATED (~1U)
3734
3735static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3736{
3737 struct irq_remap_table *table = NULL;
3738 struct amd_iommu *iommu;
3739 unsigned long flags;
3740 u16 alias;
3741
3742 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3743
3744 iommu = amd_iommu_rlookup_table[devid];
3745 if (!iommu)
3746 goto out_unlock;
3747
3748 table = irq_lookup_table[devid];
3749 if (table)
3750 goto out;
3751
3752 alias = amd_iommu_alias_table[devid];
3753 table = irq_lookup_table[alias];
3754 if (table) {
3755 irq_lookup_table[devid] = table;
3756 set_dte_irq_entry(devid, table);
3757 iommu_flush_dte(iommu, devid);
3758 goto out;
3759 }
3760
3761 /* Nothing there yet, allocate new irq remapping table */
3762 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3763 if (!table)
3764 goto out;
3765
Joerg Roedel197887f2013-04-09 21:14:08 +02003766 /* Initialize table spin-lock */
3767 spin_lock_init(&table->lock);
3768
Joerg Roedel2b324502012-06-21 16:29:10 +02003769 if (ioapic)
3770 /* Keep the first 32 indexes free for IOAPIC interrupts */
3771 table->min_index = 32;
3772
3773 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3774 if (!table->table) {
3775 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003776 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003777 goto out;
3778 }
3779
3780 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3781
3782 if (ioapic) {
3783 int i;
3784
3785 for (i = 0; i < 32; ++i)
3786 table->table[i] = IRTE_ALLOCATED;
3787 }
3788
3789 irq_lookup_table[devid] = table;
3790 set_dte_irq_entry(devid, table);
3791 iommu_flush_dte(iommu, devid);
3792 if (devid != alias) {
3793 irq_lookup_table[alias] = table;
Alex Williamsone028a9e2014-04-22 10:08:40 -06003794 set_dte_irq_entry(alias, table);
Joerg Roedel2b324502012-06-21 16:29:10 +02003795 iommu_flush_dte(iommu, alias);
3796 }
3797
3798out:
3799 iommu_completion_wait(iommu);
3800
3801out_unlock:
3802 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3803
3804 return table;
3805}
3806
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003807static int alloc_irq_index(u16 devid, int count)
Joerg Roedel2b324502012-06-21 16:29:10 +02003808{
3809 struct irq_remap_table *table;
3810 unsigned long flags;
3811 int index, c;
3812
3813 table = get_irq_table(devid, false);
3814 if (!table)
3815 return -ENODEV;
3816
3817 spin_lock_irqsave(&table->lock, flags);
3818
3819 /* Scan table for free entries */
3820 for (c = 0, index = table->min_index;
3821 index < MAX_IRQS_PER_TABLE;
3822 ++index) {
3823 if (table->table[index] == 0)
3824 c += 1;
3825 else
3826 c = 0;
3827
3828 if (c == count) {
Joerg Roedel2b324502012-06-21 16:29:10 +02003829 for (; c != 0; --c)
3830 table->table[index - c + 1] = IRTE_ALLOCATED;
3831
3832 index -= count - 1;
Joerg Roedel2b324502012-06-21 16:29:10 +02003833 goto out;
3834 }
3835 }
3836
3837 index = -ENOSPC;
3838
3839out:
3840 spin_unlock_irqrestore(&table->lock, flags);
3841
3842 return index;
3843}
3844
Joerg Roedel2b324502012-06-21 16:29:10 +02003845static int modify_irte(u16 devid, int index, union irte irte)
3846{
3847 struct irq_remap_table *table;
3848 struct amd_iommu *iommu;
3849 unsigned long flags;
3850
3851 iommu = amd_iommu_rlookup_table[devid];
3852 if (iommu == NULL)
3853 return -EINVAL;
3854
3855 table = get_irq_table(devid, false);
3856 if (!table)
3857 return -ENOMEM;
3858
3859 spin_lock_irqsave(&table->lock, flags);
3860 table->table[index] = irte.val;
3861 spin_unlock_irqrestore(&table->lock, flags);
3862
3863 iommu_flush_irt(iommu, devid);
3864 iommu_completion_wait(iommu);
3865
3866 return 0;
3867}
3868
3869static void free_irte(u16 devid, int index)
3870{
3871 struct irq_remap_table *table;
3872 struct amd_iommu *iommu;
3873 unsigned long flags;
3874
3875 iommu = amd_iommu_rlookup_table[devid];
3876 if (iommu == NULL)
3877 return;
3878
3879 table = get_irq_table(devid, false);
3880 if (!table)
3881 return;
3882
3883 spin_lock_irqsave(&table->lock, flags);
3884 table->table[index] = 0;
3885 spin_unlock_irqrestore(&table->lock, flags);
3886
3887 iommu_flush_irt(iommu, devid);
3888 iommu_completion_wait(iommu);
3889}
3890
Jiang Liu7c71d302015-04-13 14:11:33 +08003891static int get_devid(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003892{
Jiang Liu7c71d302015-04-13 14:11:33 +08003893 int devid = -1;
Joerg Roedel5527de72012-06-26 11:17:32 +02003894
Jiang Liu7c71d302015-04-13 14:11:33 +08003895 switch (info->type) {
3896 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3897 devid = get_ioapic_devid(info->ioapic_id);
3898 break;
3899 case X86_IRQ_ALLOC_TYPE_HPET:
3900 devid = get_hpet_devid(info->hpet_id);
3901 break;
3902 case X86_IRQ_ALLOC_TYPE_MSI:
3903 case X86_IRQ_ALLOC_TYPE_MSIX:
3904 devid = get_device_id(&info->msi_dev->dev);
3905 break;
3906 default:
3907 BUG_ON(1);
3908 break;
Joerg Roedel5527de72012-06-26 11:17:32 +02003909 }
3910
Jiang Liu7c71d302015-04-13 14:11:33 +08003911 return devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003912}
3913
Jiang Liu7c71d302015-04-13 14:11:33 +08003914static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003915{
Jiang Liu7c71d302015-04-13 14:11:33 +08003916 struct amd_iommu *iommu;
3917 int devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003918
Jiang Liu7c71d302015-04-13 14:11:33 +08003919 if (!info)
3920 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003921
Jiang Liu7c71d302015-04-13 14:11:33 +08003922 devid = get_devid(info);
3923 if (devid >= 0) {
3924 iommu = amd_iommu_rlookup_table[devid];
3925 if (iommu)
3926 return iommu->ir_domain;
3927 }
Joerg Roedel5527de72012-06-26 11:17:32 +02003928
Jiang Liu7c71d302015-04-13 14:11:33 +08003929 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003930}
3931
Jiang Liu7c71d302015-04-13 14:11:33 +08003932static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003933{
Jiang Liu7c71d302015-04-13 14:11:33 +08003934 struct amd_iommu *iommu;
3935 int devid;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003936
Jiang Liu7c71d302015-04-13 14:11:33 +08003937 if (!info)
3938 return NULL;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003939
Jiang Liu7c71d302015-04-13 14:11:33 +08003940 switch (info->type) {
3941 case X86_IRQ_ALLOC_TYPE_MSI:
3942 case X86_IRQ_ALLOC_TYPE_MSIX:
3943 devid = get_device_id(&info->msi_dev->dev);
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003944 if (IS_ERR_VALUE(devid))
3945 return NULL;
3946
Dan Carpenter1fb260b2016-01-07 12:36:06 +03003947 iommu = amd_iommu_rlookup_table[devid];
3948 if (iommu)
3949 return iommu->msi_domain;
Jiang Liu7c71d302015-04-13 14:11:33 +08003950 break;
3951 default:
3952 break;
3953 }
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003954
Jiang Liu7c71d302015-04-13 14:11:33 +08003955 return NULL;
Joerg Roedeld9761952012-06-26 16:00:08 +02003956}
3957
Joerg Roedel6b474b82012-06-26 16:46:04 +02003958struct irq_remap_ops amd_iommu_irq_ops = {
Joerg Roedel6b474b82012-06-26 16:46:04 +02003959 .prepare = amd_iommu_prepare,
3960 .enable = amd_iommu_enable,
3961 .disable = amd_iommu_disable,
3962 .reenable = amd_iommu_reenable,
3963 .enable_faulting = amd_iommu_enable_faulting,
Jiang Liu7c71d302015-04-13 14:11:33 +08003964 .get_ir_irq_domain = get_ir_irq_domain,
3965 .get_irq_domain = get_irq_domain,
Joerg Roedel6b474b82012-06-26 16:46:04 +02003966};
Jiang Liu7c71d302015-04-13 14:11:33 +08003967
3968static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3969 struct irq_cfg *irq_cfg,
3970 struct irq_alloc_info *info,
3971 int devid, int index, int sub_handle)
3972{
3973 struct irq_2_irte *irte_info = &data->irq_2_irte;
3974 struct msi_msg *msg = &data->msi_entry;
3975 union irte *irte = &data->irte_entry;
3976 struct IO_APIC_route_entry *entry;
3977
Jiang Liu7c71d302015-04-13 14:11:33 +08003978 data->irq_2_irte.devid = devid;
3979 data->irq_2_irte.index = index + sub_handle;
3980
3981 /* Setup IRTE for IOMMU */
3982 irte->val = 0;
3983 irte->fields.vector = irq_cfg->vector;
3984 irte->fields.int_type = apic->irq_delivery_mode;
3985 irte->fields.destination = irq_cfg->dest_apicid;
3986 irte->fields.dm = apic->irq_dest_mode;
3987 irte->fields.valid = 1;
3988
3989 switch (info->type) {
3990 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3991 /* Setup IOAPIC entry */
3992 entry = info->ioapic_entry;
3993 info->ioapic_entry = NULL;
3994 memset(entry, 0, sizeof(*entry));
3995 entry->vector = index;
3996 entry->mask = 0;
3997 entry->trigger = info->ioapic_trigger;
3998 entry->polarity = info->ioapic_polarity;
3999 /* Mask level triggered irqs. */
4000 if (info->ioapic_trigger)
4001 entry->mask = 1;
4002 break;
4003
4004 case X86_IRQ_ALLOC_TYPE_HPET:
4005 case X86_IRQ_ALLOC_TYPE_MSI:
4006 case X86_IRQ_ALLOC_TYPE_MSIX:
4007 msg->address_hi = MSI_ADDR_BASE_HI;
4008 msg->address_lo = MSI_ADDR_BASE_LO;
4009 msg->data = irte_info->index;
4010 break;
4011
4012 default:
4013 BUG_ON(1);
4014 break;
4015 }
4016}
4017
4018static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
4019 unsigned int nr_irqs, void *arg)
4020{
4021 struct irq_alloc_info *info = arg;
4022 struct irq_data *irq_data;
4023 struct amd_ir_data *data;
4024 struct irq_cfg *cfg;
4025 int i, ret, devid;
4026 int index = -1;
4027
4028 if (!info)
4029 return -EINVAL;
4030 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
4031 info->type != X86_IRQ_ALLOC_TYPE_MSIX)
4032 return -EINVAL;
4033
4034 /*
4035 * With IRQ remapping enabled, don't need contiguous CPU vectors
4036 * to support multiple MSI interrupts.
4037 */
4038 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
4039 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
4040
4041 devid = get_devid(info);
4042 if (devid < 0)
4043 return -EINVAL;
4044
4045 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
4046 if (ret < 0)
4047 return ret;
4048
Jiang Liu7c71d302015-04-13 14:11:33 +08004049 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
4050 if (get_irq_table(devid, true))
4051 index = info->ioapic_pin;
4052 else
4053 ret = -ENOMEM;
4054 } else {
Jiang Liu3c3d4f92015-04-13 14:11:38 +08004055 index = alloc_irq_index(devid, nr_irqs);
Jiang Liu7c71d302015-04-13 14:11:33 +08004056 }
4057 if (index < 0) {
4058 pr_warn("Failed to allocate IRTE\n");
Jiang Liu7c71d302015-04-13 14:11:33 +08004059 goto out_free_parent;
4060 }
4061
4062 for (i = 0; i < nr_irqs; i++) {
4063 irq_data = irq_domain_get_irq_data(domain, virq + i);
4064 cfg = irqd_cfg(irq_data);
4065 if (!irq_data || !cfg) {
4066 ret = -EINVAL;
4067 goto out_free_data;
4068 }
4069
Joerg Roedela130e692015-08-13 11:07:25 +02004070 ret = -ENOMEM;
4071 data = kzalloc(sizeof(*data), GFP_KERNEL);
4072 if (!data)
4073 goto out_free_data;
4074
Jiang Liu7c71d302015-04-13 14:11:33 +08004075 irq_data->hwirq = (devid << 16) + i;
4076 irq_data->chip_data = data;
4077 irq_data->chip = &amd_ir_chip;
4078 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
4079 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
4080 }
Joerg Roedela130e692015-08-13 11:07:25 +02004081
Jiang Liu7c71d302015-04-13 14:11:33 +08004082 return 0;
4083
4084out_free_data:
4085 for (i--; i >= 0; i--) {
4086 irq_data = irq_domain_get_irq_data(domain, virq + i);
4087 if (irq_data)
4088 kfree(irq_data->chip_data);
4089 }
4090 for (i = 0; i < nr_irqs; i++)
4091 free_irte(devid, index + i);
4092out_free_parent:
4093 irq_domain_free_irqs_common(domain, virq, nr_irqs);
4094 return ret;
4095}
4096
4097static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
4098 unsigned int nr_irqs)
4099{
4100 struct irq_2_irte *irte_info;
4101 struct irq_data *irq_data;
4102 struct amd_ir_data *data;
4103 int i;
4104
4105 for (i = 0; i < nr_irqs; i++) {
4106 irq_data = irq_domain_get_irq_data(domain, virq + i);
4107 if (irq_data && irq_data->chip_data) {
4108 data = irq_data->chip_data;
4109 irte_info = &data->irq_2_irte;
4110 free_irte(irte_info->devid, irte_info->index);
4111 kfree(data);
4112 }
4113 }
4114 irq_domain_free_irqs_common(domain, virq, nr_irqs);
4115}
4116
4117static void irq_remapping_activate(struct irq_domain *domain,
4118 struct irq_data *irq_data)
4119{
4120 struct amd_ir_data *data = irq_data->chip_data;
4121 struct irq_2_irte *irte_info = &data->irq_2_irte;
4122
4123 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
4124}
4125
4126static void irq_remapping_deactivate(struct irq_domain *domain,
4127 struct irq_data *irq_data)
4128{
4129 struct amd_ir_data *data = irq_data->chip_data;
4130 struct irq_2_irte *irte_info = &data->irq_2_irte;
4131 union irte entry;
4132
4133 entry.val = 0;
4134 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
4135}
4136
4137static struct irq_domain_ops amd_ir_domain_ops = {
4138 .alloc = irq_remapping_alloc,
4139 .free = irq_remapping_free,
4140 .activate = irq_remapping_activate,
4141 .deactivate = irq_remapping_deactivate,
4142};
4143
4144static int amd_ir_set_affinity(struct irq_data *data,
4145 const struct cpumask *mask, bool force)
4146{
4147 struct amd_ir_data *ir_data = data->chip_data;
4148 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
4149 struct irq_cfg *cfg = irqd_cfg(data);
4150 struct irq_data *parent = data->parent_data;
4151 int ret;
4152
4153 ret = parent->chip->irq_set_affinity(parent, mask, force);
4154 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
4155 return ret;
4156
4157 /*
4158 * Atomically updates the IRTE with the new destination, vector
4159 * and flushes the interrupt entry cache.
4160 */
4161 ir_data->irte_entry.fields.vector = cfg->vector;
4162 ir_data->irte_entry.fields.destination = cfg->dest_apicid;
4163 modify_irte(irte_info->devid, irte_info->index, ir_data->irte_entry);
4164
4165 /*
4166 * After this point, all the interrupts will start arriving
4167 * at the new destination. So, time to cleanup the previous
4168 * vector allocation.
4169 */
Jiang Liuc6c20022015-04-14 10:30:02 +08004170 send_cleanup_vector(cfg);
Jiang Liu7c71d302015-04-13 14:11:33 +08004171
4172 return IRQ_SET_MASK_OK_DONE;
4173}
4174
4175static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
4176{
4177 struct amd_ir_data *ir_data = irq_data->chip_data;
4178
4179 *msg = ir_data->msi_entry;
4180}
4181
4182static struct irq_chip amd_ir_chip = {
4183 .irq_ack = ir_ack_apic_edge,
4184 .irq_set_affinity = amd_ir_set_affinity,
4185 .irq_compose_msi_msg = ir_compose_msi_msg,
4186};
4187
4188int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
4189{
4190 iommu->ir_domain = irq_domain_add_tree(NULL, &amd_ir_domain_ops, iommu);
4191 if (!iommu->ir_domain)
4192 return -ENOMEM;
4193
4194 iommu->ir_domain->parent = arch_get_ir_parent_domain();
4195 iommu->msi_domain = arch_create_msi_irq_domain(iommu->ir_domain);
4196
4197 return 0;
4198}
Joerg Roedel2b324502012-06-21 16:29:10 +02004199#endif