amd_iommu.c 58 KB

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