amd_iommu.c 61 KB

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