amd_iommu.c 59 KB

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