amd_iommu.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722
  1. /*
  2. * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <joerg.roedel@amd.com>
  4. * 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. #include <linux/pci.h>
  20. #include <linux/pci-ats.h>
  21. #include <linux/bitmap.h>
  22. #include <linux/slab.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/iommu-helper.h>
  27. #include <linux/iommu.h>
  28. #include <linux/delay.h>
  29. #include <asm/proto.h>
  30. #include <asm/iommu.h>
  31. #include <asm/gart.h>
  32. #include <asm/amd_iommu_proto.h>
  33. #include <asm/amd_iommu_types.h>
  34. #include <asm/amd_iommu.h>
  35. #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
  36. #define LOOP_TIMEOUT 100000
  37. static DEFINE_RWLOCK(amd_iommu_devtable_lock);
  38. /* A list of preallocated protection domains */
  39. static LIST_HEAD(iommu_pd_list);
  40. static DEFINE_SPINLOCK(iommu_pd_list_lock);
  41. /*
  42. * Domain for untranslated devices - only allocated
  43. * if iommu=pt passed on kernel cmd line.
  44. */
  45. static struct protection_domain *pt_domain;
  46. static struct iommu_ops amd_iommu_ops;
  47. /*
  48. * general struct to manage commands send to an IOMMU
  49. */
  50. struct iommu_cmd {
  51. u32 data[4];
  52. };
  53. static void update_domain(struct protection_domain *domain);
  54. /****************************************************************************
  55. *
  56. * Helper functions
  57. *
  58. ****************************************************************************/
  59. static inline u16 get_device_id(struct device *dev)
  60. {
  61. struct pci_dev *pdev = to_pci_dev(dev);
  62. return calc_devid(pdev->bus->number, pdev->devfn);
  63. }
  64. static struct iommu_dev_data *get_dev_data(struct device *dev)
  65. {
  66. return dev->archdata.iommu;
  67. }
  68. /*
  69. * In this function the list of preallocated protection domains is traversed to
  70. * find the domain for a specific device
  71. */
  72. static struct dma_ops_domain *find_protection_domain(u16 devid)
  73. {
  74. struct dma_ops_domain *entry, *ret = NULL;
  75. unsigned long flags;
  76. u16 alias = amd_iommu_alias_table[devid];
  77. if (list_empty(&iommu_pd_list))
  78. return NULL;
  79. spin_lock_irqsave(&iommu_pd_list_lock, flags);
  80. list_for_each_entry(entry, &iommu_pd_list, list) {
  81. if (entry->target_dev == devid ||
  82. entry->target_dev == alias) {
  83. ret = entry;
  84. break;
  85. }
  86. }
  87. spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
  88. return ret;
  89. }
  90. /*
  91. * This function checks if the driver got a valid device from the caller to
  92. * avoid dereferencing invalid pointers.
  93. */
  94. static bool check_device(struct device *dev)
  95. {
  96. u16 devid;
  97. if (!dev || !dev->dma_mask)
  98. return false;
  99. /* No device or no PCI device */
  100. if (dev->bus != &pci_bus_type)
  101. return false;
  102. devid = get_device_id(dev);
  103. /* Out of our scope? */
  104. if (devid > amd_iommu_last_bdf)
  105. return false;
  106. if (amd_iommu_rlookup_table[devid] == NULL)
  107. return false;
  108. return true;
  109. }
  110. static int iommu_init_device(struct device *dev)
  111. {
  112. struct iommu_dev_data *dev_data;
  113. struct pci_dev *pdev;
  114. u16 devid, alias;
  115. if (dev->archdata.iommu)
  116. return 0;
  117. dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
  118. if (!dev_data)
  119. return -ENOMEM;
  120. dev_data->dev = dev;
  121. devid = get_device_id(dev);
  122. alias = amd_iommu_alias_table[devid];
  123. pdev = pci_get_bus_and_slot(PCI_BUS(alias), alias & 0xff);
  124. if (pdev)
  125. dev_data->alias = &pdev->dev;
  126. atomic_set(&dev_data->bind, 0);
  127. dev->archdata.iommu = dev_data;
  128. return 0;
  129. }
  130. static void iommu_uninit_device(struct device *dev)
  131. {
  132. kfree(dev->archdata.iommu);
  133. }
  134. void __init amd_iommu_uninit_devices(void)
  135. {
  136. struct pci_dev *pdev = NULL;
  137. for_each_pci_dev(pdev) {
  138. if (!check_device(&pdev->dev))
  139. continue;
  140. iommu_uninit_device(&pdev->dev);
  141. }
  142. }
  143. int __init amd_iommu_init_devices(void)
  144. {
  145. struct pci_dev *pdev = NULL;
  146. int ret = 0;
  147. for_each_pci_dev(pdev) {
  148. if (!check_device(&pdev->dev))
  149. continue;
  150. ret = iommu_init_device(&pdev->dev);
  151. if (ret)
  152. goto out_free;
  153. }
  154. return 0;
  155. out_free:
  156. amd_iommu_uninit_devices();
  157. return ret;
  158. }
  159. #ifdef CONFIG_AMD_IOMMU_STATS
  160. /*
  161. * Initialization code for statistics collection
  162. */
  163. DECLARE_STATS_COUNTER(compl_wait);
  164. DECLARE_STATS_COUNTER(cnt_map_single);
  165. DECLARE_STATS_COUNTER(cnt_unmap_single);
  166. DECLARE_STATS_COUNTER(cnt_map_sg);
  167. DECLARE_STATS_COUNTER(cnt_unmap_sg);
  168. DECLARE_STATS_COUNTER(cnt_alloc_coherent);
  169. DECLARE_STATS_COUNTER(cnt_free_coherent);
  170. DECLARE_STATS_COUNTER(cross_page);
  171. DECLARE_STATS_COUNTER(domain_flush_single);
  172. DECLARE_STATS_COUNTER(domain_flush_all);
  173. DECLARE_STATS_COUNTER(alloced_io_mem);
  174. DECLARE_STATS_COUNTER(total_map_requests);
  175. static struct dentry *stats_dir;
  176. static struct dentry *de_fflush;
  177. static void amd_iommu_stats_add(struct __iommu_counter *cnt)
  178. {
  179. if (stats_dir == NULL)
  180. return;
  181. cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
  182. &cnt->value);
  183. }
  184. static void amd_iommu_stats_init(void)
  185. {
  186. stats_dir = debugfs_create_dir("amd-iommu", NULL);
  187. if (stats_dir == NULL)
  188. return;
  189. de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
  190. (u32 *)&amd_iommu_unmap_flush);
  191. amd_iommu_stats_add(&compl_wait);
  192. amd_iommu_stats_add(&cnt_map_single);
  193. amd_iommu_stats_add(&cnt_unmap_single);
  194. amd_iommu_stats_add(&cnt_map_sg);
  195. amd_iommu_stats_add(&cnt_unmap_sg);
  196. amd_iommu_stats_add(&cnt_alloc_coherent);
  197. amd_iommu_stats_add(&cnt_free_coherent);
  198. amd_iommu_stats_add(&cross_page);
  199. amd_iommu_stats_add(&domain_flush_single);
  200. amd_iommu_stats_add(&domain_flush_all);
  201. amd_iommu_stats_add(&alloced_io_mem);
  202. amd_iommu_stats_add(&total_map_requests);
  203. }
  204. #endif
  205. /****************************************************************************
  206. *
  207. * Interrupt handling functions
  208. *
  209. ****************************************************************************/
  210. static void dump_dte_entry(u16 devid)
  211. {
  212. int i;
  213. for (i = 0; i < 8; ++i)
  214. pr_err("AMD-Vi: DTE[%d]: %08x\n", i,
  215. amd_iommu_dev_table[devid].data[i]);
  216. }
  217. static void dump_command(unsigned long phys_addr)
  218. {
  219. struct iommu_cmd *cmd = phys_to_virt(phys_addr);
  220. int i;
  221. for (i = 0; i < 4; ++i)
  222. pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
  223. }
  224. static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
  225. {
  226. u32 *event = __evt;
  227. int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  228. int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  229. int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
  230. int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  231. u64 address = (u64)(((u64)event[3]) << 32) | event[2];
  232. printk(KERN_ERR "AMD-Vi: Event logged [");
  233. switch (type) {
  234. case EVENT_TYPE_ILL_DEV:
  235. printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
  236. "address=0x%016llx flags=0x%04x]\n",
  237. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  238. address, flags);
  239. dump_dte_entry(devid);
  240. break;
  241. case EVENT_TYPE_IO_FAULT:
  242. printk("IO_PAGE_FAULT device=%02x:%02x.%x "
  243. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  244. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  245. domid, address, flags);
  246. break;
  247. case EVENT_TYPE_DEV_TAB_ERR:
  248. printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  249. "address=0x%016llx flags=0x%04x]\n",
  250. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  251. address, flags);
  252. break;
  253. case EVENT_TYPE_PAGE_TAB_ERR:
  254. printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  255. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  256. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  257. domid, address, flags);
  258. break;
  259. case EVENT_TYPE_ILL_CMD:
  260. printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
  261. dump_command(address);
  262. break;
  263. case EVENT_TYPE_CMD_HARD_ERR:
  264. printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
  265. "flags=0x%04x]\n", address, flags);
  266. break;
  267. case EVENT_TYPE_IOTLB_INV_TO:
  268. printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
  269. "address=0x%016llx]\n",
  270. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  271. address);
  272. break;
  273. case EVENT_TYPE_INV_DEV_REQ:
  274. printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
  275. "address=0x%016llx flags=0x%04x]\n",
  276. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  277. address, flags);
  278. break;
  279. default:
  280. printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
  281. }
  282. }
  283. static void iommu_poll_events(struct amd_iommu *iommu)
  284. {
  285. u32 head, tail;
  286. unsigned long flags;
  287. spin_lock_irqsave(&iommu->lock, flags);
  288. head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  289. tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  290. while (head != tail) {
  291. iommu_print_event(iommu, iommu->evt_buf + head);
  292. head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
  293. }
  294. writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  295. spin_unlock_irqrestore(&iommu->lock, flags);
  296. }
  297. irqreturn_t amd_iommu_int_thread(int irq, void *data)
  298. {
  299. struct amd_iommu *iommu;
  300. for_each_iommu(iommu)
  301. iommu_poll_events(iommu);
  302. return IRQ_HANDLED;
  303. }
  304. irqreturn_t amd_iommu_int_handler(int irq, void *data)
  305. {
  306. return IRQ_WAKE_THREAD;
  307. }
  308. /****************************************************************************
  309. *
  310. * IOMMU command queuing functions
  311. *
  312. ****************************************************************************/
  313. static int wait_on_sem(volatile u64 *sem)
  314. {
  315. int i = 0;
  316. while (*sem == 0 && i < LOOP_TIMEOUT) {
  317. udelay(1);
  318. i += 1;
  319. }
  320. if (i == LOOP_TIMEOUT) {
  321. pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
  322. return -EIO;
  323. }
  324. return 0;
  325. }
  326. static void copy_cmd_to_buffer(struct amd_iommu *iommu,
  327. struct iommu_cmd *cmd,
  328. u32 tail)
  329. {
  330. u8 *target;
  331. target = iommu->cmd_buf + tail;
  332. tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
  333. /* Copy command to buffer */
  334. memcpy(target, cmd, sizeof(*cmd));
  335. /* Tell the IOMMU about it */
  336. writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  337. }
  338. static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
  339. {
  340. WARN_ON(address & 0x7ULL);
  341. memset(cmd, 0, sizeof(*cmd));
  342. cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
  343. cmd->data[1] = upper_32_bits(__pa(address));
  344. cmd->data[2] = 1;
  345. CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
  346. }
  347. static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
  348. {
  349. memset(cmd, 0, sizeof(*cmd));
  350. cmd->data[0] = devid;
  351. CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
  352. }
  353. static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
  354. size_t size, u16 domid, int pde)
  355. {
  356. u64 pages;
  357. int s;
  358. pages = iommu_num_pages(address, size, PAGE_SIZE);
  359. s = 0;
  360. if (pages > 1) {
  361. /*
  362. * If we have to flush more than one page, flush all
  363. * TLB entries for this domain
  364. */
  365. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  366. s = 1;
  367. }
  368. address &= PAGE_MASK;
  369. memset(cmd, 0, sizeof(*cmd));
  370. cmd->data[1] |= domid;
  371. cmd->data[2] = lower_32_bits(address);
  372. cmd->data[3] = upper_32_bits(address);
  373. CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
  374. if (s) /* size bit - we flush more than one 4kb page */
  375. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  376. if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
  377. cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  378. }
  379. static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
  380. u64 address, size_t size)
  381. {
  382. u64 pages;
  383. int s;
  384. pages = iommu_num_pages(address, size, PAGE_SIZE);
  385. s = 0;
  386. if (pages > 1) {
  387. /*
  388. * If we have to flush more than one page, flush all
  389. * TLB entries for this domain
  390. */
  391. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  392. s = 1;
  393. }
  394. address &= PAGE_MASK;
  395. memset(cmd, 0, sizeof(*cmd));
  396. cmd->data[0] = devid;
  397. cmd->data[0] |= (qdep & 0xff) << 24;
  398. cmd->data[1] = devid;
  399. cmd->data[2] = lower_32_bits(address);
  400. cmd->data[3] = upper_32_bits(address);
  401. CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
  402. if (s)
  403. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  404. }
  405. static void build_inv_all(struct iommu_cmd *cmd)
  406. {
  407. memset(cmd, 0, sizeof(*cmd));
  408. CMD_SET_TYPE(cmd, CMD_INV_ALL);
  409. }
  410. /*
  411. * Writes the command to the IOMMUs command buffer and informs the
  412. * hardware about the new command.
  413. */
  414. static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  415. {
  416. u32 left, tail, head, next_tail;
  417. unsigned long flags;
  418. WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
  419. again:
  420. spin_lock_irqsave(&iommu->lock, flags);
  421. head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  422. tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  423. next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
  424. left = (head - next_tail) % iommu->cmd_buf_size;
  425. if (left <= 2) {
  426. struct iommu_cmd sync_cmd;
  427. volatile u64 sem = 0;
  428. int ret;
  429. build_completion_wait(&sync_cmd, (u64)&sem);
  430. copy_cmd_to_buffer(iommu, &sync_cmd, tail);
  431. spin_unlock_irqrestore(&iommu->lock, flags);
  432. if ((ret = wait_on_sem(&sem)) != 0)
  433. return ret;
  434. goto again;
  435. }
  436. copy_cmd_to_buffer(iommu, cmd, tail);
  437. /* We need to sync now to make sure all commands are processed */
  438. iommu->need_sync = true;
  439. spin_unlock_irqrestore(&iommu->lock, flags);
  440. return 0;
  441. }
  442. /*
  443. * This function queues a completion wait command into the command
  444. * buffer of an IOMMU
  445. */
  446. static int iommu_completion_wait(struct amd_iommu *iommu)
  447. {
  448. struct iommu_cmd cmd;
  449. volatile u64 sem = 0;
  450. int ret;
  451. if (!iommu->need_sync)
  452. return 0;
  453. build_completion_wait(&cmd, (u64)&sem);
  454. ret = iommu_queue_command(iommu, &cmd);
  455. if (ret)
  456. return ret;
  457. return wait_on_sem(&sem);
  458. }
  459. static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
  460. {
  461. struct iommu_cmd cmd;
  462. build_inv_dte(&cmd, devid);
  463. return iommu_queue_command(iommu, &cmd);
  464. }
  465. static void iommu_flush_dte_all(struct amd_iommu *iommu)
  466. {
  467. u32 devid;
  468. for (devid = 0; devid <= 0xffff; ++devid)
  469. iommu_flush_dte(iommu, devid);
  470. iommu_completion_wait(iommu);
  471. }
  472. /*
  473. * This function uses heavy locking and may disable irqs for some time. But
  474. * this is no issue because it is only called during resume.
  475. */
  476. static void iommu_flush_tlb_all(struct amd_iommu *iommu)
  477. {
  478. u32 dom_id;
  479. for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
  480. struct iommu_cmd cmd;
  481. build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  482. dom_id, 1);
  483. iommu_queue_command(iommu, &cmd);
  484. }
  485. iommu_completion_wait(iommu);
  486. }
  487. static void iommu_flush_all(struct amd_iommu *iommu)
  488. {
  489. struct iommu_cmd cmd;
  490. build_inv_all(&cmd);
  491. iommu_queue_command(iommu, &cmd);
  492. iommu_completion_wait(iommu);
  493. }
  494. void iommu_flush_all_caches(struct amd_iommu *iommu)
  495. {
  496. if (iommu_feature(iommu, FEATURE_IA)) {
  497. iommu_flush_all(iommu);
  498. } else {
  499. iommu_flush_dte_all(iommu);
  500. iommu_flush_tlb_all(iommu);
  501. }
  502. }
  503. /*
  504. * Command send function for flushing on-device TLB
  505. */
  506. static int device_flush_iotlb(struct device *dev, u64 address, size_t size)
  507. {
  508. struct pci_dev *pdev = to_pci_dev(dev);
  509. struct amd_iommu *iommu;
  510. struct iommu_cmd cmd;
  511. u16 devid;
  512. int qdep;
  513. qdep = pci_ats_queue_depth(pdev);
  514. devid = get_device_id(dev);
  515. iommu = amd_iommu_rlookup_table[devid];
  516. build_inv_iotlb_pages(&cmd, devid, qdep, address, size);
  517. return iommu_queue_command(iommu, &cmd);
  518. }
  519. /*
  520. * Command send function for invalidating a device table entry
  521. */
  522. static int device_flush_dte(struct device *dev)
  523. {
  524. struct amd_iommu *iommu;
  525. struct pci_dev *pdev;
  526. u16 devid;
  527. int ret;
  528. pdev = to_pci_dev(dev);
  529. devid = get_device_id(dev);
  530. iommu = amd_iommu_rlookup_table[devid];
  531. ret = iommu_flush_dte(iommu, devid);
  532. if (ret)
  533. return ret;
  534. if (pci_ats_enabled(pdev))
  535. ret = device_flush_iotlb(dev, 0, ~0UL);
  536. return ret;
  537. }
  538. /*
  539. * TLB invalidation function which is called from the mapping functions.
  540. * It invalidates a single PTE if the range to flush is within a single
  541. * page. Otherwise it flushes the whole TLB of the IOMMU.
  542. */
  543. static void __domain_flush_pages(struct protection_domain *domain,
  544. u64 address, size_t size, int pde)
  545. {
  546. struct iommu_dev_data *dev_data;
  547. struct iommu_cmd cmd;
  548. int ret = 0, i;
  549. build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
  550. for (i = 0; i < amd_iommus_present; ++i) {
  551. if (!domain->dev_iommu[i])
  552. continue;
  553. /*
  554. * Devices of this domain are behind this IOMMU
  555. * We need a TLB flush
  556. */
  557. ret |= iommu_queue_command(amd_iommus[i], &cmd);
  558. }
  559. list_for_each_entry(dev_data, &domain->dev_list, list) {
  560. struct pci_dev *pdev = to_pci_dev(dev_data->dev);
  561. if (!pci_ats_enabled(pdev))
  562. continue;
  563. ret |= device_flush_iotlb(dev_data->dev, address, size);
  564. }
  565. WARN_ON(ret);
  566. }
  567. static void domain_flush_pages(struct protection_domain *domain,
  568. u64 address, size_t size)
  569. {
  570. __domain_flush_pages(domain, address, size, 0);
  571. }
  572. /* Flush the whole IO/TLB for a given protection domain */
  573. static void domain_flush_tlb(struct protection_domain *domain)
  574. {
  575. __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
  576. }
  577. /* Flush the whole IO/TLB for a given protection domain - including PDE */
  578. static void domain_flush_tlb_pde(struct protection_domain *domain)
  579. {
  580. __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
  581. }
  582. static void domain_flush_complete(struct protection_domain *domain)
  583. {
  584. int i;
  585. for (i = 0; i < amd_iommus_present; ++i) {
  586. if (!domain->dev_iommu[i])
  587. continue;
  588. /*
  589. * Devices of this domain are behind this IOMMU
  590. * We need to wait for completion of all commands.
  591. */
  592. iommu_completion_wait(amd_iommus[i]);
  593. }
  594. }
  595. /*
  596. * This function flushes the DTEs for all devices in domain
  597. */
  598. static void domain_flush_devices(struct protection_domain *domain)
  599. {
  600. struct iommu_dev_data *dev_data;
  601. unsigned long flags;
  602. spin_lock_irqsave(&domain->lock, flags);
  603. list_for_each_entry(dev_data, &domain->dev_list, list)
  604. device_flush_dte(dev_data->dev);
  605. spin_unlock_irqrestore(&domain->lock, flags);
  606. }
  607. /****************************************************************************
  608. *
  609. * The functions below are used the create the page table mappings for
  610. * unity mapped regions.
  611. *
  612. ****************************************************************************/
  613. /*
  614. * This function is used to add another level to an IO page table. Adding
  615. * another level increases the size of the address space by 9 bits to a size up
  616. * to 64 bits.
  617. */
  618. static bool increase_address_space(struct protection_domain *domain,
  619. gfp_t gfp)
  620. {
  621. u64 *pte;
  622. if (domain->mode == PAGE_MODE_6_LEVEL)
  623. /* address space already 64 bit large */
  624. return false;
  625. pte = (void *)get_zeroed_page(gfp);
  626. if (!pte)
  627. return false;
  628. *pte = PM_LEVEL_PDE(domain->mode,
  629. virt_to_phys(domain->pt_root));
  630. domain->pt_root = pte;
  631. domain->mode += 1;
  632. domain->updated = true;
  633. return true;
  634. }
  635. static u64 *alloc_pte(struct protection_domain *domain,
  636. unsigned long address,
  637. unsigned long page_size,
  638. u64 **pte_page,
  639. gfp_t gfp)
  640. {
  641. int level, end_lvl;
  642. u64 *pte, *page;
  643. BUG_ON(!is_power_of_2(page_size));
  644. while (address > PM_LEVEL_SIZE(domain->mode))
  645. increase_address_space(domain, gfp);
  646. level = domain->mode - 1;
  647. pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
  648. address = PAGE_SIZE_ALIGN(address, page_size);
  649. end_lvl = PAGE_SIZE_LEVEL(page_size);
  650. while (level > end_lvl) {
  651. if (!IOMMU_PTE_PRESENT(*pte)) {
  652. page = (u64 *)get_zeroed_page(gfp);
  653. if (!page)
  654. return NULL;
  655. *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
  656. }
  657. /* No level skipping support yet */
  658. if (PM_PTE_LEVEL(*pte) != level)
  659. return NULL;
  660. level -= 1;
  661. pte = IOMMU_PTE_PAGE(*pte);
  662. if (pte_page && level == end_lvl)
  663. *pte_page = pte;
  664. pte = &pte[PM_LEVEL_INDEX(level, address)];
  665. }
  666. return pte;
  667. }
  668. /*
  669. * This function checks if there is a PTE for a given dma address. If
  670. * there is one, it returns the pointer to it.
  671. */
  672. static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
  673. {
  674. int level;
  675. u64 *pte;
  676. if (address > PM_LEVEL_SIZE(domain->mode))
  677. return NULL;
  678. level = domain->mode - 1;
  679. pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
  680. while (level > 0) {
  681. /* Not Present */
  682. if (!IOMMU_PTE_PRESENT(*pte))
  683. return NULL;
  684. /* Large PTE */
  685. if (PM_PTE_LEVEL(*pte) == 0x07) {
  686. unsigned long pte_mask, __pte;
  687. /*
  688. * If we have a series of large PTEs, make
  689. * sure to return a pointer to the first one.
  690. */
  691. pte_mask = PTE_PAGE_SIZE(*pte);
  692. pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
  693. __pte = ((unsigned long)pte) & pte_mask;
  694. return (u64 *)__pte;
  695. }
  696. /* No level skipping support yet */
  697. if (PM_PTE_LEVEL(*pte) != level)
  698. return NULL;
  699. level -= 1;
  700. /* Walk to the next level */
  701. pte = IOMMU_PTE_PAGE(*pte);
  702. pte = &pte[PM_LEVEL_INDEX(level, address)];
  703. }
  704. return pte;
  705. }
  706. /*
  707. * Generic mapping functions. It maps a physical address into a DMA
  708. * address space. It allocates the page table pages if necessary.
  709. * In the future it can be extended to a generic mapping function
  710. * supporting all features of AMD IOMMU page tables like level skipping
  711. * and full 64 bit address spaces.
  712. */
  713. static int iommu_map_page(struct protection_domain *dom,
  714. unsigned long bus_addr,
  715. unsigned long phys_addr,
  716. int prot,
  717. unsigned long page_size)
  718. {
  719. u64 __pte, *pte;
  720. int i, count;
  721. if (!(prot & IOMMU_PROT_MASK))
  722. return -EINVAL;
  723. bus_addr = PAGE_ALIGN(bus_addr);
  724. phys_addr = PAGE_ALIGN(phys_addr);
  725. count = PAGE_SIZE_PTE_COUNT(page_size);
  726. pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
  727. for (i = 0; i < count; ++i)
  728. if (IOMMU_PTE_PRESENT(pte[i]))
  729. return -EBUSY;
  730. if (page_size > PAGE_SIZE) {
  731. __pte = PAGE_SIZE_PTE(phys_addr, page_size);
  732. __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
  733. } else
  734. __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
  735. if (prot & IOMMU_PROT_IR)
  736. __pte |= IOMMU_PTE_IR;
  737. if (prot & IOMMU_PROT_IW)
  738. __pte |= IOMMU_PTE_IW;
  739. for (i = 0; i < count; ++i)
  740. pte[i] = __pte;
  741. update_domain(dom);
  742. return 0;
  743. }
  744. static unsigned long iommu_unmap_page(struct protection_domain *dom,
  745. unsigned long bus_addr,
  746. unsigned long page_size)
  747. {
  748. unsigned long long unmap_size, unmapped;
  749. u64 *pte;
  750. BUG_ON(!is_power_of_2(page_size));
  751. unmapped = 0;
  752. while (unmapped < page_size) {
  753. pte = fetch_pte(dom, bus_addr);
  754. if (!pte) {
  755. /*
  756. * No PTE for this address
  757. * move forward in 4kb steps
  758. */
  759. unmap_size = PAGE_SIZE;
  760. } else if (PM_PTE_LEVEL(*pte) == 0) {
  761. /* 4kb PTE found for this address */
  762. unmap_size = PAGE_SIZE;
  763. *pte = 0ULL;
  764. } else {
  765. int count, i;
  766. /* Large PTE found which maps this address */
  767. unmap_size = PTE_PAGE_SIZE(*pte);
  768. count = PAGE_SIZE_PTE_COUNT(unmap_size);
  769. for (i = 0; i < count; i++)
  770. pte[i] = 0ULL;
  771. }
  772. bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
  773. unmapped += unmap_size;
  774. }
  775. BUG_ON(!is_power_of_2(unmapped));
  776. return unmapped;
  777. }
  778. /*
  779. * This function checks if a specific unity mapping entry is needed for
  780. * this specific IOMMU.
  781. */
  782. static int iommu_for_unity_map(struct amd_iommu *iommu,
  783. struct unity_map_entry *entry)
  784. {
  785. u16 bdf, i;
  786. for (i = entry->devid_start; i <= entry->devid_end; ++i) {
  787. bdf = amd_iommu_alias_table[i];
  788. if (amd_iommu_rlookup_table[bdf] == iommu)
  789. return 1;
  790. }
  791. return 0;
  792. }
  793. /*
  794. * This function actually applies the mapping to the page table of the
  795. * dma_ops domain.
  796. */
  797. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  798. struct unity_map_entry *e)
  799. {
  800. u64 addr;
  801. int ret;
  802. for (addr = e->address_start; addr < e->address_end;
  803. addr += PAGE_SIZE) {
  804. ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
  805. PAGE_SIZE);
  806. if (ret)
  807. return ret;
  808. /*
  809. * if unity mapping is in aperture range mark the page
  810. * as allocated in the aperture
  811. */
  812. if (addr < dma_dom->aperture_size)
  813. __set_bit(addr >> PAGE_SHIFT,
  814. dma_dom->aperture[0]->bitmap);
  815. }
  816. return 0;
  817. }
  818. /*
  819. * Init the unity mappings for a specific IOMMU in the system
  820. *
  821. * Basically iterates over all unity mapping entries and applies them to
  822. * the default domain DMA of that IOMMU if necessary.
  823. */
  824. static int iommu_init_unity_mappings(struct amd_iommu *iommu)
  825. {
  826. struct unity_map_entry *entry;
  827. int ret;
  828. list_for_each_entry(entry, &amd_iommu_unity_map, list) {
  829. if (!iommu_for_unity_map(iommu, entry))
  830. continue;
  831. ret = dma_ops_unity_map(iommu->default_dom, entry);
  832. if (ret)
  833. return ret;
  834. }
  835. return 0;
  836. }
  837. /*
  838. * Inits the unity mappings required for a specific device
  839. */
  840. static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
  841. u16 devid)
  842. {
  843. struct unity_map_entry *e;
  844. int ret;
  845. list_for_each_entry(e, &amd_iommu_unity_map, list) {
  846. if (!(devid >= e->devid_start && devid <= e->devid_end))
  847. continue;
  848. ret = dma_ops_unity_map(dma_dom, e);
  849. if (ret)
  850. return ret;
  851. }
  852. return 0;
  853. }
  854. /****************************************************************************
  855. *
  856. * The next functions belong to the address allocator for the dma_ops
  857. * interface functions. They work like the allocators in the other IOMMU
  858. * drivers. Its basically a bitmap which marks the allocated pages in
  859. * the aperture. Maybe it could be enhanced in the future to a more
  860. * efficient allocator.
  861. *
  862. ****************************************************************************/
  863. /*
  864. * The address allocator core functions.
  865. *
  866. * called with domain->lock held
  867. */
  868. /*
  869. * Used to reserve address ranges in the aperture (e.g. for exclusion
  870. * ranges.
  871. */
  872. static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
  873. unsigned long start_page,
  874. unsigned int pages)
  875. {
  876. unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
  877. if (start_page + pages > last_page)
  878. pages = last_page - start_page;
  879. for (i = start_page; i < start_page + pages; ++i) {
  880. int index = i / APERTURE_RANGE_PAGES;
  881. int page = i % APERTURE_RANGE_PAGES;
  882. __set_bit(page, dom->aperture[index]->bitmap);
  883. }
  884. }
  885. /*
  886. * This function is used to add a new aperture range to an existing
  887. * aperture in case of dma_ops domain allocation or address allocation
  888. * failure.
  889. */
  890. static int alloc_new_range(struct dma_ops_domain *dma_dom,
  891. bool populate, gfp_t gfp)
  892. {
  893. int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
  894. struct amd_iommu *iommu;
  895. unsigned long i;
  896. #ifdef CONFIG_IOMMU_STRESS
  897. populate = false;
  898. #endif
  899. if (index >= APERTURE_MAX_RANGES)
  900. return -ENOMEM;
  901. dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
  902. if (!dma_dom->aperture[index])
  903. return -ENOMEM;
  904. dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
  905. if (!dma_dom->aperture[index]->bitmap)
  906. goto out_free;
  907. dma_dom->aperture[index]->offset = dma_dom->aperture_size;
  908. if (populate) {
  909. unsigned long address = dma_dom->aperture_size;
  910. int i, num_ptes = APERTURE_RANGE_PAGES / 512;
  911. u64 *pte, *pte_page;
  912. for (i = 0; i < num_ptes; ++i) {
  913. pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
  914. &pte_page, gfp);
  915. if (!pte)
  916. goto out_free;
  917. dma_dom->aperture[index]->pte_pages[i] = pte_page;
  918. address += APERTURE_RANGE_SIZE / 64;
  919. }
  920. }
  921. dma_dom->aperture_size += APERTURE_RANGE_SIZE;
  922. /* Initialize the exclusion range if necessary */
  923. for_each_iommu(iommu) {
  924. if (iommu->exclusion_start &&
  925. iommu->exclusion_start >= dma_dom->aperture[index]->offset
  926. && iommu->exclusion_start < dma_dom->aperture_size) {
  927. unsigned long startpage;
  928. int pages = iommu_num_pages(iommu->exclusion_start,
  929. iommu->exclusion_length,
  930. PAGE_SIZE);
  931. startpage = iommu->exclusion_start >> PAGE_SHIFT;
  932. dma_ops_reserve_addresses(dma_dom, startpage, pages);
  933. }
  934. }
  935. /*
  936. * Check for areas already mapped as present in the new aperture
  937. * range and mark those pages as reserved in the allocator. Such
  938. * mappings may already exist as a result of requested unity
  939. * mappings for devices.
  940. */
  941. for (i = dma_dom->aperture[index]->offset;
  942. i < dma_dom->aperture_size;
  943. i += PAGE_SIZE) {
  944. u64 *pte = fetch_pte(&dma_dom->domain, i);
  945. if (!pte || !IOMMU_PTE_PRESENT(*pte))
  946. continue;
  947. dma_ops_reserve_addresses(dma_dom, i << PAGE_SHIFT, 1);
  948. }
  949. update_domain(&dma_dom->domain);
  950. return 0;
  951. out_free:
  952. update_domain(&dma_dom->domain);
  953. free_page((unsigned long)dma_dom->aperture[index]->bitmap);
  954. kfree(dma_dom->aperture[index]);
  955. dma_dom->aperture[index] = NULL;
  956. return -ENOMEM;
  957. }
  958. static unsigned long dma_ops_area_alloc(struct device *dev,
  959. struct dma_ops_domain *dom,
  960. unsigned int pages,
  961. unsigned long align_mask,
  962. u64 dma_mask,
  963. unsigned long start)
  964. {
  965. unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
  966. int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
  967. int i = start >> APERTURE_RANGE_SHIFT;
  968. unsigned long boundary_size;
  969. unsigned long address = -1;
  970. unsigned long limit;
  971. next_bit >>= PAGE_SHIFT;
  972. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  973. PAGE_SIZE) >> PAGE_SHIFT;
  974. for (;i < max_index; ++i) {
  975. unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
  976. if (dom->aperture[i]->offset >= dma_mask)
  977. break;
  978. limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
  979. dma_mask >> PAGE_SHIFT);
  980. address = iommu_area_alloc(dom->aperture[i]->bitmap,
  981. limit, next_bit, pages, 0,
  982. boundary_size, align_mask);
  983. if (address != -1) {
  984. address = dom->aperture[i]->offset +
  985. (address << PAGE_SHIFT);
  986. dom->next_address = address + (pages << PAGE_SHIFT);
  987. break;
  988. }
  989. next_bit = 0;
  990. }
  991. return address;
  992. }
  993. static unsigned long dma_ops_alloc_addresses(struct device *dev,
  994. struct dma_ops_domain *dom,
  995. unsigned int pages,
  996. unsigned long align_mask,
  997. u64 dma_mask)
  998. {
  999. unsigned long address;
  1000. #ifdef CONFIG_IOMMU_STRESS
  1001. dom->next_address = 0;
  1002. dom->need_flush = true;
  1003. #endif
  1004. address = dma_ops_area_alloc(dev, dom, pages, align_mask,
  1005. dma_mask, dom->next_address);
  1006. if (address == -1) {
  1007. dom->next_address = 0;
  1008. address = dma_ops_area_alloc(dev, dom, pages, align_mask,
  1009. dma_mask, 0);
  1010. dom->need_flush = true;
  1011. }
  1012. if (unlikely(address == -1))
  1013. address = DMA_ERROR_CODE;
  1014. WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
  1015. return address;
  1016. }
  1017. /*
  1018. * The address free function.
  1019. *
  1020. * called with domain->lock held
  1021. */
  1022. static void dma_ops_free_addresses(struct dma_ops_domain *dom,
  1023. unsigned long address,
  1024. unsigned int pages)
  1025. {
  1026. unsigned i = address >> APERTURE_RANGE_SHIFT;
  1027. struct aperture_range *range = dom->aperture[i];
  1028. BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
  1029. #ifdef CONFIG_IOMMU_STRESS
  1030. if (i < 4)
  1031. return;
  1032. #endif
  1033. if (address >= dom->next_address)
  1034. dom->need_flush = true;
  1035. address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
  1036. bitmap_clear(range->bitmap, address, pages);
  1037. }
  1038. /****************************************************************************
  1039. *
  1040. * The next functions belong to the domain allocation. A domain is
  1041. * allocated for every IOMMU as the default domain. If device isolation
  1042. * is enabled, every device get its own domain. The most important thing
  1043. * about domains is the page table mapping the DMA address space they
  1044. * contain.
  1045. *
  1046. ****************************************************************************/
  1047. /*
  1048. * This function adds a protection domain to the global protection domain list
  1049. */
  1050. static void add_domain_to_list(struct protection_domain *domain)
  1051. {
  1052. unsigned long flags;
  1053. spin_lock_irqsave(&amd_iommu_pd_lock, flags);
  1054. list_add(&domain->list, &amd_iommu_pd_list);
  1055. spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
  1056. }
  1057. /*
  1058. * This function removes a protection domain to the global
  1059. * protection domain list
  1060. */
  1061. static void del_domain_from_list(struct protection_domain *domain)
  1062. {
  1063. unsigned long flags;
  1064. spin_lock_irqsave(&amd_iommu_pd_lock, flags);
  1065. list_del(&domain->list);
  1066. spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
  1067. }
  1068. static u16 domain_id_alloc(void)
  1069. {
  1070. unsigned long flags;
  1071. int id;
  1072. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1073. id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
  1074. BUG_ON(id == 0);
  1075. if (id > 0 && id < MAX_DOMAIN_ID)
  1076. __set_bit(id, amd_iommu_pd_alloc_bitmap);
  1077. else
  1078. id = 0;
  1079. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1080. return id;
  1081. }
  1082. static void domain_id_free(int id)
  1083. {
  1084. unsigned long flags;
  1085. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1086. if (id > 0 && id < MAX_DOMAIN_ID)
  1087. __clear_bit(id, amd_iommu_pd_alloc_bitmap);
  1088. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1089. }
  1090. static void free_pagetable(struct protection_domain *domain)
  1091. {
  1092. int i, j;
  1093. u64 *p1, *p2, *p3;
  1094. p1 = domain->pt_root;
  1095. if (!p1)
  1096. return;
  1097. for (i = 0; i < 512; ++i) {
  1098. if (!IOMMU_PTE_PRESENT(p1[i]))
  1099. continue;
  1100. p2 = IOMMU_PTE_PAGE(p1[i]);
  1101. for (j = 0; j < 512; ++j) {
  1102. if (!IOMMU_PTE_PRESENT(p2[j]))
  1103. continue;
  1104. p3 = IOMMU_PTE_PAGE(p2[j]);
  1105. free_page((unsigned long)p3);
  1106. }
  1107. free_page((unsigned long)p2);
  1108. }
  1109. free_page((unsigned long)p1);
  1110. domain->pt_root = NULL;
  1111. }
  1112. /*
  1113. * Free a domain, only used if something went wrong in the
  1114. * allocation path and we need to free an already allocated page table
  1115. */
  1116. static void dma_ops_domain_free(struct dma_ops_domain *dom)
  1117. {
  1118. int i;
  1119. if (!dom)
  1120. return;
  1121. del_domain_from_list(&dom->domain);
  1122. free_pagetable(&dom->domain);
  1123. for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
  1124. if (!dom->aperture[i])
  1125. continue;
  1126. free_page((unsigned long)dom->aperture[i]->bitmap);
  1127. kfree(dom->aperture[i]);
  1128. }
  1129. kfree(dom);
  1130. }
  1131. /*
  1132. * Allocates a new protection domain usable for the dma_ops functions.
  1133. * It also initializes the page table and the address allocator data
  1134. * structures required for the dma_ops interface
  1135. */
  1136. static struct dma_ops_domain *dma_ops_domain_alloc(void)
  1137. {
  1138. struct dma_ops_domain *dma_dom;
  1139. dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
  1140. if (!dma_dom)
  1141. return NULL;
  1142. spin_lock_init(&dma_dom->domain.lock);
  1143. dma_dom->domain.id = domain_id_alloc();
  1144. if (dma_dom->domain.id == 0)
  1145. goto free_dma_dom;
  1146. INIT_LIST_HEAD(&dma_dom->domain.dev_list);
  1147. dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
  1148. dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  1149. dma_dom->domain.flags = PD_DMA_OPS_MASK;
  1150. dma_dom->domain.priv = dma_dom;
  1151. if (!dma_dom->domain.pt_root)
  1152. goto free_dma_dom;
  1153. dma_dom->need_flush = false;
  1154. dma_dom->target_dev = 0xffff;
  1155. add_domain_to_list(&dma_dom->domain);
  1156. if (alloc_new_range(dma_dom, true, GFP_KERNEL))
  1157. goto free_dma_dom;
  1158. /*
  1159. * mark the first page as allocated so we never return 0 as
  1160. * a valid dma-address. So we can use 0 as error value
  1161. */
  1162. dma_dom->aperture[0]->bitmap[0] = 1;
  1163. dma_dom->next_address = 0;
  1164. return dma_dom;
  1165. free_dma_dom:
  1166. dma_ops_domain_free(dma_dom);
  1167. return NULL;
  1168. }
  1169. /*
  1170. * little helper function to check whether a given protection domain is a
  1171. * dma_ops domain
  1172. */
  1173. static bool dma_ops_domain(struct protection_domain *domain)
  1174. {
  1175. return domain->flags & PD_DMA_OPS_MASK;
  1176. }
  1177. static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
  1178. {
  1179. u64 pte_root = virt_to_phys(domain->pt_root);
  1180. u32 flags = 0;
  1181. pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
  1182. << DEV_ENTRY_MODE_SHIFT;
  1183. pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
  1184. if (ats)
  1185. flags |= DTE_FLAG_IOTLB;
  1186. amd_iommu_dev_table[devid].data[3] |= flags;
  1187. amd_iommu_dev_table[devid].data[2] = domain->id;
  1188. amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
  1189. amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
  1190. }
  1191. static void clear_dte_entry(u16 devid)
  1192. {
  1193. /* remove entry from the device table seen by the hardware */
  1194. amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
  1195. amd_iommu_dev_table[devid].data[1] = 0;
  1196. amd_iommu_dev_table[devid].data[2] = 0;
  1197. amd_iommu_apply_erratum_63(devid);
  1198. }
  1199. static void do_attach(struct device *dev, struct protection_domain *domain)
  1200. {
  1201. struct iommu_dev_data *dev_data;
  1202. struct amd_iommu *iommu;
  1203. struct pci_dev *pdev;
  1204. bool ats = false;
  1205. u16 devid;
  1206. devid = get_device_id(dev);
  1207. iommu = amd_iommu_rlookup_table[devid];
  1208. dev_data = get_dev_data(dev);
  1209. pdev = to_pci_dev(dev);
  1210. if (amd_iommu_iotlb_sup)
  1211. ats = pci_ats_enabled(pdev);
  1212. /* Update data structures */
  1213. dev_data->domain = domain;
  1214. list_add(&dev_data->list, &domain->dev_list);
  1215. set_dte_entry(devid, domain, ats);
  1216. /* Do reference counting */
  1217. domain->dev_iommu[iommu->index] += 1;
  1218. domain->dev_cnt += 1;
  1219. /* Flush the DTE entry */
  1220. device_flush_dte(dev);
  1221. }
  1222. static void do_detach(struct device *dev)
  1223. {
  1224. struct iommu_dev_data *dev_data;
  1225. struct amd_iommu *iommu;
  1226. u16 devid;
  1227. devid = get_device_id(dev);
  1228. iommu = amd_iommu_rlookup_table[devid];
  1229. dev_data = get_dev_data(dev);
  1230. /* decrease reference counters */
  1231. dev_data->domain->dev_iommu[iommu->index] -= 1;
  1232. dev_data->domain->dev_cnt -= 1;
  1233. /* Update data structures */
  1234. dev_data->domain = NULL;
  1235. list_del(&dev_data->list);
  1236. clear_dte_entry(devid);
  1237. /* Flush the DTE entry */
  1238. device_flush_dte(dev);
  1239. }
  1240. /*
  1241. * If a device is not yet associated with a domain, this function does
  1242. * assigns it visible for the hardware
  1243. */
  1244. static int __attach_device(struct device *dev,
  1245. struct protection_domain *domain)
  1246. {
  1247. struct iommu_dev_data *dev_data, *alias_data;
  1248. int ret;
  1249. dev_data = get_dev_data(dev);
  1250. alias_data = get_dev_data(dev_data->alias);
  1251. if (!alias_data)
  1252. return -EINVAL;
  1253. /* lock domain */
  1254. spin_lock(&domain->lock);
  1255. /* Some sanity checks */
  1256. ret = -EBUSY;
  1257. if (alias_data->domain != NULL &&
  1258. alias_data->domain != domain)
  1259. goto out_unlock;
  1260. if (dev_data->domain != NULL &&
  1261. dev_data->domain != domain)
  1262. goto out_unlock;
  1263. /* Do real assignment */
  1264. if (dev_data->alias != dev) {
  1265. alias_data = get_dev_data(dev_data->alias);
  1266. if (alias_data->domain == NULL)
  1267. do_attach(dev_data->alias, domain);
  1268. atomic_inc(&alias_data->bind);
  1269. }
  1270. if (dev_data->domain == NULL)
  1271. do_attach(dev, domain);
  1272. atomic_inc(&dev_data->bind);
  1273. ret = 0;
  1274. out_unlock:
  1275. /* ready */
  1276. spin_unlock(&domain->lock);
  1277. return ret;
  1278. }
  1279. /*
  1280. * If a device is not yet associated with a domain, this function does
  1281. * assigns it visible for the hardware
  1282. */
  1283. static int attach_device(struct device *dev,
  1284. struct protection_domain *domain)
  1285. {
  1286. struct pci_dev *pdev = to_pci_dev(dev);
  1287. unsigned long flags;
  1288. int ret;
  1289. if (amd_iommu_iotlb_sup)
  1290. pci_enable_ats(pdev, PAGE_SHIFT);
  1291. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1292. ret = __attach_device(dev, domain);
  1293. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1294. /*
  1295. * We might boot into a crash-kernel here. The crashed kernel
  1296. * left the caches in the IOMMU dirty. So we have to flush
  1297. * here to evict all dirty stuff.
  1298. */
  1299. domain_flush_tlb_pde(domain);
  1300. return ret;
  1301. }
  1302. /*
  1303. * Removes a device from a protection domain (unlocked)
  1304. */
  1305. static void __detach_device(struct device *dev)
  1306. {
  1307. struct iommu_dev_data *dev_data = get_dev_data(dev);
  1308. struct iommu_dev_data *alias_data;
  1309. struct protection_domain *domain;
  1310. unsigned long flags;
  1311. BUG_ON(!dev_data->domain);
  1312. domain = dev_data->domain;
  1313. spin_lock_irqsave(&domain->lock, flags);
  1314. if (dev_data->alias != dev) {
  1315. alias_data = get_dev_data(dev_data->alias);
  1316. if (atomic_dec_and_test(&alias_data->bind))
  1317. do_detach(dev_data->alias);
  1318. }
  1319. if (atomic_dec_and_test(&dev_data->bind))
  1320. do_detach(dev);
  1321. spin_unlock_irqrestore(&domain->lock, flags);
  1322. /*
  1323. * If we run in passthrough mode the device must be assigned to the
  1324. * passthrough domain if it is detached from any other domain.
  1325. * Make sure we can deassign from the pt_domain itself.
  1326. */
  1327. if (iommu_pass_through &&
  1328. (dev_data->domain == NULL && domain != pt_domain))
  1329. __attach_device(dev, pt_domain);
  1330. }
  1331. /*
  1332. * Removes a device from a protection domain (with devtable_lock held)
  1333. */
  1334. static void detach_device(struct device *dev)
  1335. {
  1336. struct pci_dev *pdev = to_pci_dev(dev);
  1337. unsigned long flags;
  1338. /* lock device table */
  1339. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1340. __detach_device(dev);
  1341. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1342. if (amd_iommu_iotlb_sup && pci_ats_enabled(pdev))
  1343. pci_disable_ats(pdev);
  1344. }
  1345. /*
  1346. * Find out the protection domain structure for a given PCI device. This
  1347. * will give us the pointer to the page table root for example.
  1348. */
  1349. static struct protection_domain *domain_for_device(struct device *dev)
  1350. {
  1351. struct protection_domain *dom;
  1352. struct iommu_dev_data *dev_data, *alias_data;
  1353. unsigned long flags;
  1354. u16 devid;
  1355. devid = get_device_id(dev);
  1356. dev_data = get_dev_data(dev);
  1357. alias_data = get_dev_data(dev_data->alias);
  1358. if (!alias_data)
  1359. return NULL;
  1360. read_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1361. dom = dev_data->domain;
  1362. if (dom == NULL &&
  1363. alias_data->domain != NULL) {
  1364. __attach_device(dev, alias_data->domain);
  1365. dom = alias_data->domain;
  1366. }
  1367. read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1368. return dom;
  1369. }
  1370. static int device_change_notifier(struct notifier_block *nb,
  1371. unsigned long action, void *data)
  1372. {
  1373. struct device *dev = data;
  1374. u16 devid;
  1375. struct protection_domain *domain;
  1376. struct dma_ops_domain *dma_domain;
  1377. struct amd_iommu *iommu;
  1378. unsigned long flags;
  1379. if (!check_device(dev))
  1380. return 0;
  1381. devid = get_device_id(dev);
  1382. iommu = amd_iommu_rlookup_table[devid];
  1383. switch (action) {
  1384. case BUS_NOTIFY_UNBOUND_DRIVER:
  1385. domain = domain_for_device(dev);
  1386. if (!domain)
  1387. goto out;
  1388. if (iommu_pass_through)
  1389. break;
  1390. detach_device(dev);
  1391. break;
  1392. case BUS_NOTIFY_ADD_DEVICE:
  1393. iommu_init_device(dev);
  1394. domain = domain_for_device(dev);
  1395. /* allocate a protection domain if a device is added */
  1396. dma_domain = find_protection_domain(devid);
  1397. if (dma_domain)
  1398. goto out;
  1399. dma_domain = dma_ops_domain_alloc();
  1400. if (!dma_domain)
  1401. goto out;
  1402. dma_domain->target_dev = devid;
  1403. spin_lock_irqsave(&iommu_pd_list_lock, flags);
  1404. list_add_tail(&dma_domain->list, &iommu_pd_list);
  1405. spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
  1406. break;
  1407. case BUS_NOTIFY_DEL_DEVICE:
  1408. iommu_uninit_device(dev);
  1409. default:
  1410. goto out;
  1411. }
  1412. device_flush_dte(dev);
  1413. iommu_completion_wait(iommu);
  1414. out:
  1415. return 0;
  1416. }
  1417. static struct notifier_block device_nb = {
  1418. .notifier_call = device_change_notifier,
  1419. };
  1420. void amd_iommu_init_notifier(void)
  1421. {
  1422. bus_register_notifier(&pci_bus_type, &device_nb);
  1423. }
  1424. /*****************************************************************************
  1425. *
  1426. * The next functions belong to the dma_ops mapping/unmapping code.
  1427. *
  1428. *****************************************************************************/
  1429. /*
  1430. * In the dma_ops path we only have the struct device. This function
  1431. * finds the corresponding IOMMU, the protection domain and the
  1432. * requestor id for a given device.
  1433. * If the device is not yet associated with a domain this is also done
  1434. * in this function.
  1435. */
  1436. static struct protection_domain *get_domain(struct device *dev)
  1437. {
  1438. struct protection_domain *domain;
  1439. struct dma_ops_domain *dma_dom;
  1440. u16 devid = get_device_id(dev);
  1441. if (!check_device(dev))
  1442. return ERR_PTR(-EINVAL);
  1443. domain = domain_for_device(dev);
  1444. if (domain != NULL && !dma_ops_domain(domain))
  1445. return ERR_PTR(-EBUSY);
  1446. if (domain != NULL)
  1447. return domain;
  1448. /* Device not bount yet - bind it */
  1449. dma_dom = find_protection_domain(devid);
  1450. if (!dma_dom)
  1451. dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
  1452. attach_device(dev, &dma_dom->domain);
  1453. DUMP_printk("Using protection domain %d for device %s\n",
  1454. dma_dom->domain.id, dev_name(dev));
  1455. return &dma_dom->domain;
  1456. }
  1457. static void update_device_table(struct protection_domain *domain)
  1458. {
  1459. struct iommu_dev_data *dev_data;
  1460. list_for_each_entry(dev_data, &domain->dev_list, list) {
  1461. struct pci_dev *pdev = to_pci_dev(dev_data->dev);
  1462. u16 devid = get_device_id(dev_data->dev);
  1463. set_dte_entry(devid, domain, pci_ats_enabled(pdev));
  1464. }
  1465. }
  1466. static void update_domain(struct protection_domain *domain)
  1467. {
  1468. if (!domain->updated)
  1469. return;
  1470. update_device_table(domain);
  1471. domain_flush_devices(domain);
  1472. domain_flush_tlb_pde(domain);
  1473. domain->updated = false;
  1474. }
  1475. /*
  1476. * This function fetches the PTE for a given address in the aperture
  1477. */
  1478. static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
  1479. unsigned long address)
  1480. {
  1481. struct aperture_range *aperture;
  1482. u64 *pte, *pte_page;
  1483. aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
  1484. if (!aperture)
  1485. return NULL;
  1486. pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
  1487. if (!pte) {
  1488. pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
  1489. GFP_ATOMIC);
  1490. aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
  1491. } else
  1492. pte += PM_LEVEL_INDEX(0, address);
  1493. update_domain(&dom->domain);
  1494. return pte;
  1495. }
  1496. /*
  1497. * This is the generic map function. It maps one 4kb page at paddr to
  1498. * the given address in the DMA address space for the domain.
  1499. */
  1500. static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
  1501. unsigned long address,
  1502. phys_addr_t paddr,
  1503. int direction)
  1504. {
  1505. u64 *pte, __pte;
  1506. WARN_ON(address > dom->aperture_size);
  1507. paddr &= PAGE_MASK;
  1508. pte = dma_ops_get_pte(dom, address);
  1509. if (!pte)
  1510. return DMA_ERROR_CODE;
  1511. __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
  1512. if (direction == DMA_TO_DEVICE)
  1513. __pte |= IOMMU_PTE_IR;
  1514. else if (direction == DMA_FROM_DEVICE)
  1515. __pte |= IOMMU_PTE_IW;
  1516. else if (direction == DMA_BIDIRECTIONAL)
  1517. __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
  1518. WARN_ON(*pte);
  1519. *pte = __pte;
  1520. return (dma_addr_t)address;
  1521. }
  1522. /*
  1523. * The generic unmapping function for on page in the DMA address space.
  1524. */
  1525. static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
  1526. unsigned long address)
  1527. {
  1528. struct aperture_range *aperture;
  1529. u64 *pte;
  1530. if (address >= dom->aperture_size)
  1531. return;
  1532. aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
  1533. if (!aperture)
  1534. return;
  1535. pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
  1536. if (!pte)
  1537. return;
  1538. pte += PM_LEVEL_INDEX(0, address);
  1539. WARN_ON(!*pte);
  1540. *pte = 0ULL;
  1541. }
  1542. /*
  1543. * This function contains common code for mapping of a physically
  1544. * contiguous memory region into DMA address space. It is used by all
  1545. * mapping functions provided with this IOMMU driver.
  1546. * Must be called with the domain lock held.
  1547. */
  1548. static dma_addr_t __map_single(struct device *dev,
  1549. struct dma_ops_domain *dma_dom,
  1550. phys_addr_t paddr,
  1551. size_t size,
  1552. int dir,
  1553. bool align,
  1554. u64 dma_mask)
  1555. {
  1556. dma_addr_t offset = paddr & ~PAGE_MASK;
  1557. dma_addr_t address, start, ret;
  1558. unsigned int pages;
  1559. unsigned long align_mask = 0;
  1560. int i;
  1561. pages = iommu_num_pages(paddr, size, PAGE_SIZE);
  1562. paddr &= PAGE_MASK;
  1563. INC_STATS_COUNTER(total_map_requests);
  1564. if (pages > 1)
  1565. INC_STATS_COUNTER(cross_page);
  1566. if (align)
  1567. align_mask = (1UL << get_order(size)) - 1;
  1568. retry:
  1569. address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
  1570. dma_mask);
  1571. if (unlikely(address == DMA_ERROR_CODE)) {
  1572. /*
  1573. * setting next_address here will let the address
  1574. * allocator only scan the new allocated range in the
  1575. * first run. This is a small optimization.
  1576. */
  1577. dma_dom->next_address = dma_dom->aperture_size;
  1578. if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
  1579. goto out;
  1580. /*
  1581. * aperture was successfully enlarged by 128 MB, try
  1582. * allocation again
  1583. */
  1584. goto retry;
  1585. }
  1586. start = address;
  1587. for (i = 0; i < pages; ++i) {
  1588. ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
  1589. if (ret == DMA_ERROR_CODE)
  1590. goto out_unmap;
  1591. paddr += PAGE_SIZE;
  1592. start += PAGE_SIZE;
  1593. }
  1594. address += offset;
  1595. ADD_STATS_COUNTER(alloced_io_mem, size);
  1596. if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
  1597. domain_flush_tlb(&dma_dom->domain);
  1598. dma_dom->need_flush = false;
  1599. } else if (unlikely(amd_iommu_np_cache))
  1600. domain_flush_pages(&dma_dom->domain, address, size);
  1601. out:
  1602. return address;
  1603. out_unmap:
  1604. for (--i; i >= 0; --i) {
  1605. start -= PAGE_SIZE;
  1606. dma_ops_domain_unmap(dma_dom, start);
  1607. }
  1608. dma_ops_free_addresses(dma_dom, address, pages);
  1609. return DMA_ERROR_CODE;
  1610. }
  1611. /*
  1612. * Does the reverse of the __map_single function. Must be called with
  1613. * the domain lock held too
  1614. */
  1615. static void __unmap_single(struct dma_ops_domain *dma_dom,
  1616. dma_addr_t dma_addr,
  1617. size_t size,
  1618. int dir)
  1619. {
  1620. dma_addr_t flush_addr;
  1621. dma_addr_t i, start;
  1622. unsigned int pages;
  1623. if ((dma_addr == DMA_ERROR_CODE) ||
  1624. (dma_addr + size > dma_dom->aperture_size))
  1625. return;
  1626. flush_addr = dma_addr;
  1627. pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  1628. dma_addr &= PAGE_MASK;
  1629. start = dma_addr;
  1630. for (i = 0; i < pages; ++i) {
  1631. dma_ops_domain_unmap(dma_dom, start);
  1632. start += PAGE_SIZE;
  1633. }
  1634. SUB_STATS_COUNTER(alloced_io_mem, size);
  1635. dma_ops_free_addresses(dma_dom, dma_addr, pages);
  1636. if (amd_iommu_unmap_flush || dma_dom->need_flush) {
  1637. domain_flush_pages(&dma_dom->domain, flush_addr, size);
  1638. dma_dom->need_flush = false;
  1639. }
  1640. }
  1641. /*
  1642. * The exported map_single function for dma_ops.
  1643. */
  1644. static dma_addr_t map_page(struct device *dev, struct page *page,
  1645. unsigned long offset, size_t size,
  1646. enum dma_data_direction dir,
  1647. struct dma_attrs *attrs)
  1648. {
  1649. unsigned long flags;
  1650. struct protection_domain *domain;
  1651. dma_addr_t addr;
  1652. u64 dma_mask;
  1653. phys_addr_t paddr = page_to_phys(page) + offset;
  1654. INC_STATS_COUNTER(cnt_map_single);
  1655. domain = get_domain(dev);
  1656. if (PTR_ERR(domain) == -EINVAL)
  1657. return (dma_addr_t)paddr;
  1658. else if (IS_ERR(domain))
  1659. return DMA_ERROR_CODE;
  1660. dma_mask = *dev->dma_mask;
  1661. spin_lock_irqsave(&domain->lock, flags);
  1662. addr = __map_single(dev, domain->priv, paddr, size, dir, false,
  1663. dma_mask);
  1664. if (addr == DMA_ERROR_CODE)
  1665. goto out;
  1666. domain_flush_complete(domain);
  1667. out:
  1668. spin_unlock_irqrestore(&domain->lock, flags);
  1669. return addr;
  1670. }
  1671. /*
  1672. * The exported unmap_single function for dma_ops.
  1673. */
  1674. static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
  1675. enum dma_data_direction dir, struct dma_attrs *attrs)
  1676. {
  1677. unsigned long flags;
  1678. struct protection_domain *domain;
  1679. INC_STATS_COUNTER(cnt_unmap_single);
  1680. domain = get_domain(dev);
  1681. if (IS_ERR(domain))
  1682. return;
  1683. spin_lock_irqsave(&domain->lock, flags);
  1684. __unmap_single(domain->priv, dma_addr, size, dir);
  1685. domain_flush_complete(domain);
  1686. spin_unlock_irqrestore(&domain->lock, flags);
  1687. }
  1688. /*
  1689. * This is a special map_sg function which is used if we should map a
  1690. * device which is not handled by an AMD IOMMU in the system.
  1691. */
  1692. static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
  1693. int nelems, int dir)
  1694. {
  1695. struct scatterlist *s;
  1696. int i;
  1697. for_each_sg(sglist, s, nelems, i) {
  1698. s->dma_address = (dma_addr_t)sg_phys(s);
  1699. s->dma_length = s->length;
  1700. }
  1701. return nelems;
  1702. }
  1703. /*
  1704. * The exported map_sg function for dma_ops (handles scatter-gather
  1705. * lists).
  1706. */
  1707. static int map_sg(struct device *dev, struct scatterlist *sglist,
  1708. int nelems, enum dma_data_direction dir,
  1709. struct dma_attrs *attrs)
  1710. {
  1711. unsigned long flags;
  1712. struct protection_domain *domain;
  1713. int i;
  1714. struct scatterlist *s;
  1715. phys_addr_t paddr;
  1716. int mapped_elems = 0;
  1717. u64 dma_mask;
  1718. INC_STATS_COUNTER(cnt_map_sg);
  1719. domain = get_domain(dev);
  1720. if (PTR_ERR(domain) == -EINVAL)
  1721. return map_sg_no_iommu(dev, sglist, nelems, dir);
  1722. else if (IS_ERR(domain))
  1723. return 0;
  1724. dma_mask = *dev->dma_mask;
  1725. spin_lock_irqsave(&domain->lock, flags);
  1726. for_each_sg(sglist, s, nelems, i) {
  1727. paddr = sg_phys(s);
  1728. s->dma_address = __map_single(dev, domain->priv,
  1729. paddr, s->length, dir, false,
  1730. dma_mask);
  1731. if (s->dma_address) {
  1732. s->dma_length = s->length;
  1733. mapped_elems++;
  1734. } else
  1735. goto unmap;
  1736. }
  1737. domain_flush_complete(domain);
  1738. out:
  1739. spin_unlock_irqrestore(&domain->lock, flags);
  1740. return mapped_elems;
  1741. unmap:
  1742. for_each_sg(sglist, s, mapped_elems, i) {
  1743. if (s->dma_address)
  1744. __unmap_single(domain->priv, s->dma_address,
  1745. s->dma_length, dir);
  1746. s->dma_address = s->dma_length = 0;
  1747. }
  1748. mapped_elems = 0;
  1749. goto out;
  1750. }
  1751. /*
  1752. * The exported map_sg function for dma_ops (handles scatter-gather
  1753. * lists).
  1754. */
  1755. static void unmap_sg(struct device *dev, struct scatterlist *sglist,
  1756. int nelems, enum dma_data_direction dir,
  1757. struct dma_attrs *attrs)
  1758. {
  1759. unsigned long flags;
  1760. struct protection_domain *domain;
  1761. struct scatterlist *s;
  1762. int i;
  1763. INC_STATS_COUNTER(cnt_unmap_sg);
  1764. domain = get_domain(dev);
  1765. if (IS_ERR(domain))
  1766. return;
  1767. spin_lock_irqsave(&domain->lock, flags);
  1768. for_each_sg(sglist, s, nelems, i) {
  1769. __unmap_single(domain->priv, s->dma_address,
  1770. s->dma_length, dir);
  1771. s->dma_address = s->dma_length = 0;
  1772. }
  1773. domain_flush_complete(domain);
  1774. spin_unlock_irqrestore(&domain->lock, flags);
  1775. }
  1776. /*
  1777. * The exported alloc_coherent function for dma_ops.
  1778. */
  1779. static void *alloc_coherent(struct device *dev, size_t size,
  1780. dma_addr_t *dma_addr, gfp_t flag)
  1781. {
  1782. unsigned long flags;
  1783. void *virt_addr;
  1784. struct protection_domain *domain;
  1785. phys_addr_t paddr;
  1786. u64 dma_mask = dev->coherent_dma_mask;
  1787. INC_STATS_COUNTER(cnt_alloc_coherent);
  1788. domain = get_domain(dev);
  1789. if (PTR_ERR(domain) == -EINVAL) {
  1790. virt_addr = (void *)__get_free_pages(flag, get_order(size));
  1791. *dma_addr = __pa(virt_addr);
  1792. return virt_addr;
  1793. } else if (IS_ERR(domain))
  1794. return NULL;
  1795. dma_mask = dev->coherent_dma_mask;
  1796. flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  1797. flag |= __GFP_ZERO;
  1798. virt_addr = (void *)__get_free_pages(flag, get_order(size));
  1799. if (!virt_addr)
  1800. return NULL;
  1801. paddr = virt_to_phys(virt_addr);
  1802. if (!dma_mask)
  1803. dma_mask = *dev->dma_mask;
  1804. spin_lock_irqsave(&domain->lock, flags);
  1805. *dma_addr = __map_single(dev, domain->priv, paddr,
  1806. size, DMA_BIDIRECTIONAL, true, dma_mask);
  1807. if (*dma_addr == DMA_ERROR_CODE) {
  1808. spin_unlock_irqrestore(&domain->lock, flags);
  1809. goto out_free;
  1810. }
  1811. domain_flush_complete(domain);
  1812. spin_unlock_irqrestore(&domain->lock, flags);
  1813. return virt_addr;
  1814. out_free:
  1815. free_pages((unsigned long)virt_addr, get_order(size));
  1816. return NULL;
  1817. }
  1818. /*
  1819. * The exported free_coherent function for dma_ops.
  1820. */
  1821. static void free_coherent(struct device *dev, size_t size,
  1822. void *virt_addr, dma_addr_t dma_addr)
  1823. {
  1824. unsigned long flags;
  1825. struct protection_domain *domain;
  1826. INC_STATS_COUNTER(cnt_free_coherent);
  1827. domain = get_domain(dev);
  1828. if (IS_ERR(domain))
  1829. goto free_mem;
  1830. spin_lock_irqsave(&domain->lock, flags);
  1831. __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
  1832. domain_flush_complete(domain);
  1833. spin_unlock_irqrestore(&domain->lock, flags);
  1834. free_mem:
  1835. free_pages((unsigned long)virt_addr, get_order(size));
  1836. }
  1837. /*
  1838. * This function is called by the DMA layer to find out if we can handle a
  1839. * particular device. It is part of the dma_ops.
  1840. */
  1841. static int amd_iommu_dma_supported(struct device *dev, u64 mask)
  1842. {
  1843. return check_device(dev);
  1844. }
  1845. /*
  1846. * The function for pre-allocating protection domains.
  1847. *
  1848. * If the driver core informs the DMA layer if a driver grabs a device
  1849. * we don't need to preallocate the protection domains anymore.
  1850. * For now we have to.
  1851. */
  1852. static void prealloc_protection_domains(void)
  1853. {
  1854. struct pci_dev *dev = NULL;
  1855. struct dma_ops_domain *dma_dom;
  1856. u16 devid;
  1857. for_each_pci_dev(dev) {
  1858. /* Do we handle this device? */
  1859. if (!check_device(&dev->dev))
  1860. continue;
  1861. /* Is there already any domain for it? */
  1862. if (domain_for_device(&dev->dev))
  1863. continue;
  1864. devid = get_device_id(&dev->dev);
  1865. dma_dom = dma_ops_domain_alloc();
  1866. if (!dma_dom)
  1867. continue;
  1868. init_unity_mappings_for_device(dma_dom, devid);
  1869. dma_dom->target_dev = devid;
  1870. attach_device(&dev->dev, &dma_dom->domain);
  1871. list_add_tail(&dma_dom->list, &iommu_pd_list);
  1872. }
  1873. }
  1874. static struct dma_map_ops amd_iommu_dma_ops = {
  1875. .alloc_coherent = alloc_coherent,
  1876. .free_coherent = free_coherent,
  1877. .map_page = map_page,
  1878. .unmap_page = unmap_page,
  1879. .map_sg = map_sg,
  1880. .unmap_sg = unmap_sg,
  1881. .dma_supported = amd_iommu_dma_supported,
  1882. };
  1883. /*
  1884. * The function which clues the AMD IOMMU driver into dma_ops.
  1885. */
  1886. void __init amd_iommu_init_api(void)
  1887. {
  1888. register_iommu(&amd_iommu_ops);
  1889. }
  1890. int __init amd_iommu_init_dma_ops(void)
  1891. {
  1892. struct amd_iommu *iommu;
  1893. int ret;
  1894. /*
  1895. * first allocate a default protection domain for every IOMMU we
  1896. * found in the system. Devices not assigned to any other
  1897. * protection domain will be assigned to the default one.
  1898. */
  1899. for_each_iommu(iommu) {
  1900. iommu->default_dom = dma_ops_domain_alloc();
  1901. if (iommu->default_dom == NULL)
  1902. return -ENOMEM;
  1903. iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
  1904. ret = iommu_init_unity_mappings(iommu);
  1905. if (ret)
  1906. goto free_domains;
  1907. }
  1908. /*
  1909. * Pre-allocate the protection domains for each device.
  1910. */
  1911. prealloc_protection_domains();
  1912. iommu_detected = 1;
  1913. swiotlb = 0;
  1914. /* Make the driver finally visible to the drivers */
  1915. dma_ops = &amd_iommu_dma_ops;
  1916. amd_iommu_stats_init();
  1917. return 0;
  1918. free_domains:
  1919. for_each_iommu(iommu) {
  1920. if (iommu->default_dom)
  1921. dma_ops_domain_free(iommu->default_dom);
  1922. }
  1923. return ret;
  1924. }
  1925. /*****************************************************************************
  1926. *
  1927. * The following functions belong to the exported interface of AMD IOMMU
  1928. *
  1929. * This interface allows access to lower level functions of the IOMMU
  1930. * like protection domain handling and assignement of devices to domains
  1931. * which is not possible with the dma_ops interface.
  1932. *
  1933. *****************************************************************************/
  1934. static void cleanup_domain(struct protection_domain *domain)
  1935. {
  1936. struct iommu_dev_data *dev_data, *next;
  1937. unsigned long flags;
  1938. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1939. list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
  1940. struct device *dev = dev_data->dev;
  1941. __detach_device(dev);
  1942. atomic_set(&dev_data->bind, 0);
  1943. }
  1944. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1945. }
  1946. static void protection_domain_free(struct protection_domain *domain)
  1947. {
  1948. if (!domain)
  1949. return;
  1950. del_domain_from_list(domain);
  1951. if (domain->id)
  1952. domain_id_free(domain->id);
  1953. kfree(domain);
  1954. }
  1955. static struct protection_domain *protection_domain_alloc(void)
  1956. {
  1957. struct protection_domain *domain;
  1958. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  1959. if (!domain)
  1960. return NULL;
  1961. spin_lock_init(&domain->lock);
  1962. mutex_init(&domain->api_lock);
  1963. domain->id = domain_id_alloc();
  1964. if (!domain->id)
  1965. goto out_err;
  1966. INIT_LIST_HEAD(&domain->dev_list);
  1967. add_domain_to_list(domain);
  1968. return domain;
  1969. out_err:
  1970. kfree(domain);
  1971. return NULL;
  1972. }
  1973. static int amd_iommu_domain_init(struct iommu_domain *dom)
  1974. {
  1975. struct protection_domain *domain;
  1976. domain = protection_domain_alloc();
  1977. if (!domain)
  1978. goto out_free;
  1979. domain->mode = PAGE_MODE_3_LEVEL;
  1980. domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  1981. if (!domain->pt_root)
  1982. goto out_free;
  1983. dom->priv = domain;
  1984. return 0;
  1985. out_free:
  1986. protection_domain_free(domain);
  1987. return -ENOMEM;
  1988. }
  1989. static void amd_iommu_domain_destroy(struct iommu_domain *dom)
  1990. {
  1991. struct protection_domain *domain = dom->priv;
  1992. if (!domain)
  1993. return;
  1994. if (domain->dev_cnt > 0)
  1995. cleanup_domain(domain);
  1996. BUG_ON(domain->dev_cnt != 0);
  1997. free_pagetable(domain);
  1998. protection_domain_free(domain);
  1999. dom->priv = NULL;
  2000. }
  2001. static void amd_iommu_detach_device(struct iommu_domain *dom,
  2002. struct device *dev)
  2003. {
  2004. struct iommu_dev_data *dev_data = dev->archdata.iommu;
  2005. struct amd_iommu *iommu;
  2006. u16 devid;
  2007. if (!check_device(dev))
  2008. return;
  2009. devid = get_device_id(dev);
  2010. if (dev_data->domain != NULL)
  2011. detach_device(dev);
  2012. iommu = amd_iommu_rlookup_table[devid];
  2013. if (!iommu)
  2014. return;
  2015. device_flush_dte(dev);
  2016. iommu_completion_wait(iommu);
  2017. }
  2018. static int amd_iommu_attach_device(struct iommu_domain *dom,
  2019. struct device *dev)
  2020. {
  2021. struct protection_domain *domain = dom->priv;
  2022. struct iommu_dev_data *dev_data;
  2023. struct amd_iommu *iommu;
  2024. int ret;
  2025. u16 devid;
  2026. if (!check_device(dev))
  2027. return -EINVAL;
  2028. dev_data = dev->archdata.iommu;
  2029. devid = get_device_id(dev);
  2030. iommu = amd_iommu_rlookup_table[devid];
  2031. if (!iommu)
  2032. return -EINVAL;
  2033. if (dev_data->domain)
  2034. detach_device(dev);
  2035. ret = attach_device(dev, domain);
  2036. iommu_completion_wait(iommu);
  2037. return ret;
  2038. }
  2039. static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
  2040. phys_addr_t paddr, int gfp_order, int iommu_prot)
  2041. {
  2042. unsigned long page_size = 0x1000UL << gfp_order;
  2043. struct protection_domain *domain = dom->priv;
  2044. int prot = 0;
  2045. int ret;
  2046. if (iommu_prot & IOMMU_READ)
  2047. prot |= IOMMU_PROT_IR;
  2048. if (iommu_prot & IOMMU_WRITE)
  2049. prot |= IOMMU_PROT_IW;
  2050. mutex_lock(&domain->api_lock);
  2051. ret = iommu_map_page(domain, iova, paddr, prot, page_size);
  2052. mutex_unlock(&domain->api_lock);
  2053. return ret;
  2054. }
  2055. static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
  2056. int gfp_order)
  2057. {
  2058. struct protection_domain *domain = dom->priv;
  2059. unsigned long page_size, unmap_size;
  2060. page_size = 0x1000UL << gfp_order;
  2061. mutex_lock(&domain->api_lock);
  2062. unmap_size = iommu_unmap_page(domain, iova, page_size);
  2063. mutex_unlock(&domain->api_lock);
  2064. domain_flush_tlb_pde(domain);
  2065. return get_order(unmap_size);
  2066. }
  2067. static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
  2068. unsigned long iova)
  2069. {
  2070. struct protection_domain *domain = dom->priv;
  2071. unsigned long offset_mask;
  2072. phys_addr_t paddr;
  2073. u64 *pte, __pte;
  2074. pte = fetch_pte(domain, iova);
  2075. if (!pte || !IOMMU_PTE_PRESENT(*pte))
  2076. return 0;
  2077. if (PM_PTE_LEVEL(*pte) == 0)
  2078. offset_mask = PAGE_SIZE - 1;
  2079. else
  2080. offset_mask = PTE_PAGE_SIZE(*pte) - 1;
  2081. __pte = *pte & PM_ADDR_MASK;
  2082. paddr = (__pte & ~offset_mask) | (iova & offset_mask);
  2083. return paddr;
  2084. }
  2085. static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
  2086. unsigned long cap)
  2087. {
  2088. switch (cap) {
  2089. case IOMMU_CAP_CACHE_COHERENCY:
  2090. return 1;
  2091. }
  2092. return 0;
  2093. }
  2094. static struct iommu_ops amd_iommu_ops = {
  2095. .domain_init = amd_iommu_domain_init,
  2096. .domain_destroy = amd_iommu_domain_destroy,
  2097. .attach_dev = amd_iommu_attach_device,
  2098. .detach_dev = amd_iommu_detach_device,
  2099. .map = amd_iommu_map,
  2100. .unmap = amd_iommu_unmap,
  2101. .iova_to_phys = amd_iommu_iova_to_phys,
  2102. .domain_has_cap = amd_iommu_domain_has_cap,
  2103. };
  2104. /*****************************************************************************
  2105. *
  2106. * The next functions do a basic initialization of IOMMU for pass through
  2107. * mode
  2108. *
  2109. * In passthrough mode the IOMMU is initialized and enabled but not used for
  2110. * DMA-API translation.
  2111. *
  2112. *****************************************************************************/
  2113. int __init amd_iommu_init_passthrough(void)
  2114. {
  2115. struct amd_iommu *iommu;
  2116. struct pci_dev *dev = NULL;
  2117. u16 devid;
  2118. /* allocate passthrough domain */
  2119. pt_domain = protection_domain_alloc();
  2120. if (!pt_domain)
  2121. return -ENOMEM;
  2122. pt_domain->mode |= PAGE_MODE_NONE;
  2123. for_each_pci_dev(dev) {
  2124. if (!check_device(&dev->dev))
  2125. continue;
  2126. devid = get_device_id(&dev->dev);
  2127. iommu = amd_iommu_rlookup_table[devid];
  2128. if (!iommu)
  2129. continue;
  2130. attach_device(&dev->dev, pt_domain);
  2131. }
  2132. pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
  2133. return 0;
  2134. }