amd_iommu.c 59 KB

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