amd_iommu.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048
  1. /*
  2. * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <jroedel@suse.de>
  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/ratelimit.h>
  20. #include <linux/pci.h>
  21. #include <linux/acpi.h>
  22. #include <linux/amba/bus.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pci-ats.h>
  25. #include <linux/bitmap.h>
  26. #include <linux/slab.h>
  27. #include <linux/debugfs.h>
  28. #include <linux/scatterlist.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/iommu-helper.h>
  31. #include <linux/iommu.h>
  32. #include <linux/delay.h>
  33. #include <linux/amd-iommu.h>
  34. #include <linux/notifier.h>
  35. #include <linux/export.h>
  36. #include <linux/irq.h>
  37. #include <linux/msi.h>
  38. #include <linux/dma-contiguous.h>
  39. #include <linux/irqdomain.h>
  40. #include <linux/percpu.h>
  41. #include <linux/iova.h>
  42. #include <asm/irq_remapping.h>
  43. #include <asm/io_apic.h>
  44. #include <asm/apic.h>
  45. #include <asm/hw_irq.h>
  46. #include <asm/msidef.h>
  47. #include <asm/proto.h>
  48. #include <asm/iommu.h>
  49. #include <asm/gart.h>
  50. #include <asm/dma.h>
  51. #include "amd_iommu_proto.h"
  52. #include "amd_iommu_types.h"
  53. #include "irq_remapping.h"
  54. #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
  55. #define LOOP_TIMEOUT 100000
  56. /* IO virtual address start page frame number */
  57. #define IOVA_START_PFN (1)
  58. #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
  59. #define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
  60. /* Reserved IOVA ranges */
  61. #define MSI_RANGE_START (0xfee00000)
  62. #define MSI_RANGE_END (0xfeefffff)
  63. #define HT_RANGE_START (0xfd00000000ULL)
  64. #define HT_RANGE_END (0xffffffffffULL)
  65. /*
  66. * This bitmap is used to advertise the page sizes our hardware support
  67. * to the IOMMU core, which will then use this information to split
  68. * physically contiguous memory regions it is mapping into page sizes
  69. * that we support.
  70. *
  71. * 512GB Pages are not supported due to a hardware bug
  72. */
  73. #define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
  74. static DEFINE_RWLOCK(amd_iommu_devtable_lock);
  75. /* List of all available dev_data structures */
  76. static LIST_HEAD(dev_data_list);
  77. static DEFINE_SPINLOCK(dev_data_list_lock);
  78. LIST_HEAD(ioapic_map);
  79. LIST_HEAD(hpet_map);
  80. LIST_HEAD(acpihid_map);
  81. #define FLUSH_QUEUE_SIZE 256
  82. struct flush_queue_entry {
  83. unsigned long iova_pfn;
  84. unsigned long pages;
  85. struct dma_ops_domain *dma_dom;
  86. };
  87. struct flush_queue {
  88. spinlock_t lock;
  89. unsigned next;
  90. struct flush_queue_entry *entries;
  91. };
  92. DEFINE_PER_CPU(struct flush_queue, flush_queue);
  93. static atomic_t queue_timer_on;
  94. static struct timer_list queue_timer;
  95. /*
  96. * Domain for untranslated devices - only allocated
  97. * if iommu=pt passed on kernel cmd line.
  98. */
  99. static const struct iommu_ops amd_iommu_ops;
  100. static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
  101. int amd_iommu_max_glx_val = -1;
  102. static struct dma_map_ops amd_iommu_dma_ops;
  103. /*
  104. * This struct contains device specific data for the IOMMU
  105. */
  106. struct iommu_dev_data {
  107. struct list_head list; /* For domain->dev_list */
  108. struct list_head dev_data_list; /* For global dev_data_list */
  109. struct protection_domain *domain; /* Domain the device is bound to */
  110. u16 devid; /* PCI Device ID */
  111. u16 alias; /* Alias Device ID */
  112. bool iommu_v2; /* Device can make use of IOMMUv2 */
  113. bool passthrough; /* Device is identity mapped */
  114. struct {
  115. bool enabled;
  116. int qdep;
  117. } ats; /* ATS state */
  118. bool pri_tlp; /* PASID TLB required for
  119. PPR completions */
  120. u32 errata; /* Bitmap for errata to apply */
  121. };
  122. /*
  123. * general struct to manage commands send to an IOMMU
  124. */
  125. struct iommu_cmd {
  126. u32 data[4];
  127. };
  128. struct kmem_cache *amd_iommu_irq_cache;
  129. static void update_domain(struct protection_domain *domain);
  130. static int protection_domain_init(struct protection_domain *domain);
  131. static void detach_device(struct device *dev);
  132. /*
  133. * Data container for a dma_ops specific protection domain
  134. */
  135. struct dma_ops_domain {
  136. /* generic protection domain information */
  137. struct protection_domain domain;
  138. /* IOVA RB-Tree */
  139. struct iova_domain iovad;
  140. };
  141. static struct iova_domain reserved_iova_ranges;
  142. static struct lock_class_key reserved_rbtree_key;
  143. /****************************************************************************
  144. *
  145. * Helper functions
  146. *
  147. ****************************************************************************/
  148. static inline int match_hid_uid(struct device *dev,
  149. struct acpihid_map_entry *entry)
  150. {
  151. const char *hid, *uid;
  152. hid = acpi_device_hid(ACPI_COMPANION(dev));
  153. uid = acpi_device_uid(ACPI_COMPANION(dev));
  154. if (!hid || !(*hid))
  155. return -ENODEV;
  156. if (!uid || !(*uid))
  157. return strcmp(hid, entry->hid);
  158. if (!(*entry->uid))
  159. return strcmp(hid, entry->hid);
  160. return (strcmp(hid, entry->hid) || strcmp(uid, entry->uid));
  161. }
  162. static inline u16 get_pci_device_id(struct device *dev)
  163. {
  164. struct pci_dev *pdev = to_pci_dev(dev);
  165. return PCI_DEVID(pdev->bus->number, pdev->devfn);
  166. }
  167. static inline int get_acpihid_device_id(struct device *dev,
  168. struct acpihid_map_entry **entry)
  169. {
  170. struct acpihid_map_entry *p;
  171. list_for_each_entry(p, &acpihid_map, list) {
  172. if (!match_hid_uid(dev, p)) {
  173. if (entry)
  174. *entry = p;
  175. return p->devid;
  176. }
  177. }
  178. return -EINVAL;
  179. }
  180. static inline int get_device_id(struct device *dev)
  181. {
  182. int devid;
  183. if (dev_is_pci(dev))
  184. devid = get_pci_device_id(dev);
  185. else
  186. devid = get_acpihid_device_id(dev, NULL);
  187. return devid;
  188. }
  189. static struct protection_domain *to_pdomain(struct iommu_domain *dom)
  190. {
  191. return container_of(dom, struct protection_domain, domain);
  192. }
  193. static struct dma_ops_domain* to_dma_ops_domain(struct protection_domain *domain)
  194. {
  195. BUG_ON(domain->flags != PD_DMA_OPS_MASK);
  196. return container_of(domain, struct dma_ops_domain, domain);
  197. }
  198. static struct iommu_dev_data *alloc_dev_data(u16 devid)
  199. {
  200. struct iommu_dev_data *dev_data;
  201. unsigned long flags;
  202. dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
  203. if (!dev_data)
  204. return NULL;
  205. dev_data->devid = devid;
  206. spin_lock_irqsave(&dev_data_list_lock, flags);
  207. list_add_tail(&dev_data->dev_data_list, &dev_data_list);
  208. spin_unlock_irqrestore(&dev_data_list_lock, flags);
  209. return dev_data;
  210. }
  211. static struct iommu_dev_data *search_dev_data(u16 devid)
  212. {
  213. struct iommu_dev_data *dev_data;
  214. unsigned long flags;
  215. spin_lock_irqsave(&dev_data_list_lock, flags);
  216. list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
  217. if (dev_data->devid == devid)
  218. goto out_unlock;
  219. }
  220. dev_data = NULL;
  221. out_unlock:
  222. spin_unlock_irqrestore(&dev_data_list_lock, flags);
  223. return dev_data;
  224. }
  225. static int __last_alias(struct pci_dev *pdev, u16 alias, void *data)
  226. {
  227. *(u16 *)data = alias;
  228. return 0;
  229. }
  230. static u16 get_alias(struct device *dev)
  231. {
  232. struct pci_dev *pdev = to_pci_dev(dev);
  233. u16 devid, ivrs_alias, pci_alias;
  234. /* The callers make sure that get_device_id() does not fail here */
  235. devid = get_device_id(dev);
  236. ivrs_alias = amd_iommu_alias_table[devid];
  237. pci_for_each_dma_alias(pdev, __last_alias, &pci_alias);
  238. if (ivrs_alias == pci_alias)
  239. return ivrs_alias;
  240. /*
  241. * DMA alias showdown
  242. *
  243. * The IVRS is fairly reliable in telling us about aliases, but it
  244. * can't know about every screwy device. If we don't have an IVRS
  245. * reported alias, use the PCI reported alias. In that case we may
  246. * still need to initialize the rlookup and dev_table entries if the
  247. * alias is to a non-existent device.
  248. */
  249. if (ivrs_alias == devid) {
  250. if (!amd_iommu_rlookup_table[pci_alias]) {
  251. amd_iommu_rlookup_table[pci_alias] =
  252. amd_iommu_rlookup_table[devid];
  253. memcpy(amd_iommu_dev_table[pci_alias].data,
  254. amd_iommu_dev_table[devid].data,
  255. sizeof(amd_iommu_dev_table[pci_alias].data));
  256. }
  257. return pci_alias;
  258. }
  259. pr_info("AMD-Vi: Using IVRS reported alias %02x:%02x.%d "
  260. "for device %s[%04x:%04x], kernel reported alias "
  261. "%02x:%02x.%d\n", PCI_BUS_NUM(ivrs_alias), PCI_SLOT(ivrs_alias),
  262. PCI_FUNC(ivrs_alias), dev_name(dev), pdev->vendor, pdev->device,
  263. PCI_BUS_NUM(pci_alias), PCI_SLOT(pci_alias),
  264. PCI_FUNC(pci_alias));
  265. /*
  266. * If we don't have a PCI DMA alias and the IVRS alias is on the same
  267. * bus, then the IVRS table may know about a quirk that we don't.
  268. */
  269. if (pci_alias == devid &&
  270. PCI_BUS_NUM(ivrs_alias) == pdev->bus->number) {
  271. pci_add_dma_alias(pdev, ivrs_alias & 0xff);
  272. pr_info("AMD-Vi: Added PCI DMA alias %02x.%d for %s\n",
  273. PCI_SLOT(ivrs_alias), PCI_FUNC(ivrs_alias),
  274. dev_name(dev));
  275. }
  276. return ivrs_alias;
  277. }
  278. static struct iommu_dev_data *find_dev_data(u16 devid)
  279. {
  280. struct iommu_dev_data *dev_data;
  281. dev_data = search_dev_data(devid);
  282. if (dev_data == NULL)
  283. dev_data = alloc_dev_data(devid);
  284. return dev_data;
  285. }
  286. static struct iommu_dev_data *get_dev_data(struct device *dev)
  287. {
  288. return dev->archdata.iommu;
  289. }
  290. /*
  291. * Find or create an IOMMU group for a acpihid device.
  292. */
  293. static struct iommu_group *acpihid_device_group(struct device *dev)
  294. {
  295. struct acpihid_map_entry *p, *entry = NULL;
  296. int devid;
  297. devid = get_acpihid_device_id(dev, &entry);
  298. if (devid < 0)
  299. return ERR_PTR(devid);
  300. list_for_each_entry(p, &acpihid_map, list) {
  301. if ((devid == p->devid) && p->group)
  302. entry->group = p->group;
  303. }
  304. if (!entry->group)
  305. entry->group = generic_device_group(dev);
  306. return entry->group;
  307. }
  308. static bool pci_iommuv2_capable(struct pci_dev *pdev)
  309. {
  310. static const int caps[] = {
  311. PCI_EXT_CAP_ID_ATS,
  312. PCI_EXT_CAP_ID_PRI,
  313. PCI_EXT_CAP_ID_PASID,
  314. };
  315. int i, pos;
  316. for (i = 0; i < 3; ++i) {
  317. pos = pci_find_ext_capability(pdev, caps[i]);
  318. if (pos == 0)
  319. return false;
  320. }
  321. return true;
  322. }
  323. static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
  324. {
  325. struct iommu_dev_data *dev_data;
  326. dev_data = get_dev_data(&pdev->dev);
  327. return dev_data->errata & (1 << erratum) ? true : false;
  328. }
  329. /*
  330. * This function checks if the driver got a valid device from the caller to
  331. * avoid dereferencing invalid pointers.
  332. */
  333. static bool check_device(struct device *dev)
  334. {
  335. int devid;
  336. if (!dev || !dev->dma_mask)
  337. return false;
  338. devid = get_device_id(dev);
  339. if (devid < 0)
  340. return false;
  341. /* Out of our scope? */
  342. if (devid > amd_iommu_last_bdf)
  343. return false;
  344. if (amd_iommu_rlookup_table[devid] == NULL)
  345. return false;
  346. return true;
  347. }
  348. static void init_iommu_group(struct device *dev)
  349. {
  350. struct iommu_group *group;
  351. group = iommu_group_get_for_dev(dev);
  352. if (IS_ERR(group))
  353. return;
  354. iommu_group_put(group);
  355. }
  356. static int iommu_init_device(struct device *dev)
  357. {
  358. struct iommu_dev_data *dev_data;
  359. int devid;
  360. if (dev->archdata.iommu)
  361. return 0;
  362. devid = get_device_id(dev);
  363. if (devid < 0)
  364. return devid;
  365. dev_data = find_dev_data(devid);
  366. if (!dev_data)
  367. return -ENOMEM;
  368. dev_data->alias = get_alias(dev);
  369. if (dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
  370. struct amd_iommu *iommu;
  371. iommu = amd_iommu_rlookup_table[dev_data->devid];
  372. dev_data->iommu_v2 = iommu->is_iommu_v2;
  373. }
  374. dev->archdata.iommu = dev_data;
  375. iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
  376. dev);
  377. return 0;
  378. }
  379. static void iommu_ignore_device(struct device *dev)
  380. {
  381. u16 alias;
  382. int devid;
  383. devid = get_device_id(dev);
  384. if (devid < 0)
  385. return;
  386. alias = get_alias(dev);
  387. memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
  388. memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
  389. amd_iommu_rlookup_table[devid] = NULL;
  390. amd_iommu_rlookup_table[alias] = NULL;
  391. }
  392. static void iommu_uninit_device(struct device *dev)
  393. {
  394. int devid;
  395. struct iommu_dev_data *dev_data;
  396. devid = get_device_id(dev);
  397. if (devid < 0)
  398. return;
  399. dev_data = search_dev_data(devid);
  400. if (!dev_data)
  401. return;
  402. if (dev_data->domain)
  403. detach_device(dev);
  404. iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
  405. dev);
  406. iommu_group_remove_device(dev);
  407. /* Remove dma-ops */
  408. dev->archdata.dma_ops = NULL;
  409. /*
  410. * We keep dev_data around for unplugged devices and reuse it when the
  411. * device is re-plugged - not doing so would introduce a ton of races.
  412. */
  413. }
  414. /****************************************************************************
  415. *
  416. * Interrupt handling functions
  417. *
  418. ****************************************************************************/
  419. static void dump_dte_entry(u16 devid)
  420. {
  421. int i;
  422. for (i = 0; i < 4; ++i)
  423. pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
  424. amd_iommu_dev_table[devid].data[i]);
  425. }
  426. static void dump_command(unsigned long phys_addr)
  427. {
  428. struct iommu_cmd *cmd = phys_to_virt(phys_addr);
  429. int i;
  430. for (i = 0; i < 4; ++i)
  431. pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
  432. }
  433. static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
  434. {
  435. int type, devid, domid, flags;
  436. volatile u32 *event = __evt;
  437. int count = 0;
  438. u64 address;
  439. retry:
  440. type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  441. devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  442. domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
  443. flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  444. address = (u64)(((u64)event[3]) << 32) | event[2];
  445. if (type == 0) {
  446. /* Did we hit the erratum? */
  447. if (++count == LOOP_TIMEOUT) {
  448. pr_err("AMD-Vi: No event written to event log\n");
  449. return;
  450. }
  451. udelay(1);
  452. goto retry;
  453. }
  454. printk(KERN_ERR "AMD-Vi: Event logged [");
  455. switch (type) {
  456. case EVENT_TYPE_ILL_DEV:
  457. printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
  458. "address=0x%016llx flags=0x%04x]\n",
  459. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  460. address, flags);
  461. dump_dte_entry(devid);
  462. break;
  463. case EVENT_TYPE_IO_FAULT:
  464. printk("IO_PAGE_FAULT device=%02x:%02x.%x "
  465. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  466. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  467. domid, address, flags);
  468. break;
  469. case EVENT_TYPE_DEV_TAB_ERR:
  470. printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  471. "address=0x%016llx flags=0x%04x]\n",
  472. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  473. address, flags);
  474. break;
  475. case EVENT_TYPE_PAGE_TAB_ERR:
  476. printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  477. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  478. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  479. domid, address, flags);
  480. break;
  481. case EVENT_TYPE_ILL_CMD:
  482. printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
  483. dump_command(address);
  484. break;
  485. case EVENT_TYPE_CMD_HARD_ERR:
  486. printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
  487. "flags=0x%04x]\n", address, flags);
  488. break;
  489. case EVENT_TYPE_IOTLB_INV_TO:
  490. printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
  491. "address=0x%016llx]\n",
  492. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  493. address);
  494. break;
  495. case EVENT_TYPE_INV_DEV_REQ:
  496. printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
  497. "address=0x%016llx flags=0x%04x]\n",
  498. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  499. address, flags);
  500. break;
  501. default:
  502. printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
  503. }
  504. memset(__evt, 0, 4 * sizeof(u32));
  505. }
  506. static void iommu_poll_events(struct amd_iommu *iommu)
  507. {
  508. u32 head, tail;
  509. head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  510. tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  511. while (head != tail) {
  512. iommu_print_event(iommu, iommu->evt_buf + head);
  513. head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
  514. }
  515. writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  516. }
  517. static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
  518. {
  519. struct amd_iommu_fault fault;
  520. if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
  521. pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
  522. return;
  523. }
  524. fault.address = raw[1];
  525. fault.pasid = PPR_PASID(raw[0]);
  526. fault.device_id = PPR_DEVID(raw[0]);
  527. fault.tag = PPR_TAG(raw[0]);
  528. fault.flags = PPR_FLAGS(raw[0]);
  529. atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
  530. }
  531. static void iommu_poll_ppr_log(struct amd_iommu *iommu)
  532. {
  533. u32 head, tail;
  534. if (iommu->ppr_log == NULL)
  535. return;
  536. head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  537. tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  538. while (head != tail) {
  539. volatile u64 *raw;
  540. u64 entry[2];
  541. int i;
  542. raw = (u64 *)(iommu->ppr_log + head);
  543. /*
  544. * Hardware bug: Interrupt may arrive before the entry is
  545. * written to memory. If this happens we need to wait for the
  546. * entry to arrive.
  547. */
  548. for (i = 0; i < LOOP_TIMEOUT; ++i) {
  549. if (PPR_REQ_TYPE(raw[0]) != 0)
  550. break;
  551. udelay(1);
  552. }
  553. /* Avoid memcpy function-call overhead */
  554. entry[0] = raw[0];
  555. entry[1] = raw[1];
  556. /*
  557. * To detect the hardware bug we need to clear the entry
  558. * back to zero.
  559. */
  560. raw[0] = raw[1] = 0UL;
  561. /* Update head pointer of hardware ring-buffer */
  562. head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
  563. writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  564. /* Handle PPR entry */
  565. iommu_handle_ppr_entry(iommu, entry);
  566. /* Refresh ring-buffer information */
  567. head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  568. tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  569. }
  570. }
  571. irqreturn_t amd_iommu_int_thread(int irq, void *data)
  572. {
  573. struct amd_iommu *iommu = (struct amd_iommu *) data;
  574. u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  575. while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
  576. /* Enable EVT and PPR interrupts again */
  577. writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
  578. iommu->mmio_base + MMIO_STATUS_OFFSET);
  579. if (status & MMIO_STATUS_EVT_INT_MASK) {
  580. pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
  581. iommu_poll_events(iommu);
  582. }
  583. if (status & MMIO_STATUS_PPR_INT_MASK) {
  584. pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
  585. iommu_poll_ppr_log(iommu);
  586. }
  587. /*
  588. * Hardware bug: ERBT1312
  589. * When re-enabling interrupt (by writing 1
  590. * to clear the bit), the hardware might also try to set
  591. * the interrupt bit in the event status register.
  592. * In this scenario, the bit will be set, and disable
  593. * subsequent interrupts.
  594. *
  595. * Workaround: The IOMMU driver should read back the
  596. * status register and check if the interrupt bits are cleared.
  597. * If not, driver will need to go through the interrupt handler
  598. * again and re-clear the bits
  599. */
  600. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  601. }
  602. return IRQ_HANDLED;
  603. }
  604. irqreturn_t amd_iommu_int_handler(int irq, void *data)
  605. {
  606. return IRQ_WAKE_THREAD;
  607. }
  608. /****************************************************************************
  609. *
  610. * IOMMU command queuing functions
  611. *
  612. ****************************************************************************/
  613. static int wait_on_sem(volatile u64 *sem)
  614. {
  615. int i = 0;
  616. while (*sem == 0 && i < LOOP_TIMEOUT) {
  617. udelay(1);
  618. i += 1;
  619. }
  620. if (i == LOOP_TIMEOUT) {
  621. pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
  622. return -EIO;
  623. }
  624. return 0;
  625. }
  626. static void copy_cmd_to_buffer(struct amd_iommu *iommu,
  627. struct iommu_cmd *cmd,
  628. u32 tail)
  629. {
  630. u8 *target;
  631. target = iommu->cmd_buf + tail;
  632. tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
  633. /* Copy command to buffer */
  634. memcpy(target, cmd, sizeof(*cmd));
  635. /* Tell the IOMMU about it */
  636. writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  637. }
  638. static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
  639. {
  640. WARN_ON(address & 0x7ULL);
  641. memset(cmd, 0, sizeof(*cmd));
  642. cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
  643. cmd->data[1] = upper_32_bits(__pa(address));
  644. cmd->data[2] = 1;
  645. CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
  646. }
  647. static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
  648. {
  649. memset(cmd, 0, sizeof(*cmd));
  650. cmd->data[0] = devid;
  651. CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
  652. }
  653. static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
  654. size_t size, u16 domid, int pde)
  655. {
  656. u64 pages;
  657. bool s;
  658. pages = iommu_num_pages(address, size, PAGE_SIZE);
  659. s = false;
  660. if (pages > 1) {
  661. /*
  662. * If we have to flush more than one page, flush all
  663. * TLB entries for this domain
  664. */
  665. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  666. s = true;
  667. }
  668. address &= PAGE_MASK;
  669. memset(cmd, 0, sizeof(*cmd));
  670. cmd->data[1] |= domid;
  671. cmd->data[2] = lower_32_bits(address);
  672. cmd->data[3] = upper_32_bits(address);
  673. CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
  674. if (s) /* size bit - we flush more than one 4kb page */
  675. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  676. if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
  677. cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  678. }
  679. static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
  680. u64 address, size_t size)
  681. {
  682. u64 pages;
  683. bool s;
  684. pages = iommu_num_pages(address, size, PAGE_SIZE);
  685. s = false;
  686. if (pages > 1) {
  687. /*
  688. * If we have to flush more than one page, flush all
  689. * TLB entries for this domain
  690. */
  691. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  692. s = true;
  693. }
  694. address &= PAGE_MASK;
  695. memset(cmd, 0, sizeof(*cmd));
  696. cmd->data[0] = devid;
  697. cmd->data[0] |= (qdep & 0xff) << 24;
  698. cmd->data[1] = devid;
  699. cmd->data[2] = lower_32_bits(address);
  700. cmd->data[3] = upper_32_bits(address);
  701. CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
  702. if (s)
  703. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  704. }
  705. static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
  706. u64 address, bool size)
  707. {
  708. memset(cmd, 0, sizeof(*cmd));
  709. address &= ~(0xfffULL);
  710. cmd->data[0] = pasid;
  711. cmd->data[1] = domid;
  712. cmd->data[2] = lower_32_bits(address);
  713. cmd->data[3] = upper_32_bits(address);
  714. cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  715. cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
  716. if (size)
  717. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  718. CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
  719. }
  720. static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
  721. int qdep, u64 address, bool size)
  722. {
  723. memset(cmd, 0, sizeof(*cmd));
  724. address &= ~(0xfffULL);
  725. cmd->data[0] = devid;
  726. cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
  727. cmd->data[0] |= (qdep & 0xff) << 24;
  728. cmd->data[1] = devid;
  729. cmd->data[1] |= (pasid & 0xff) << 16;
  730. cmd->data[2] = lower_32_bits(address);
  731. cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
  732. cmd->data[3] = upper_32_bits(address);
  733. if (size)
  734. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  735. CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
  736. }
  737. static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
  738. int status, int tag, bool gn)
  739. {
  740. memset(cmd, 0, sizeof(*cmd));
  741. cmd->data[0] = devid;
  742. if (gn) {
  743. cmd->data[1] = pasid;
  744. cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
  745. }
  746. cmd->data[3] = tag & 0x1ff;
  747. cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
  748. CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
  749. }
  750. static void build_inv_all(struct iommu_cmd *cmd)
  751. {
  752. memset(cmd, 0, sizeof(*cmd));
  753. CMD_SET_TYPE(cmd, CMD_INV_ALL);
  754. }
  755. static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
  756. {
  757. memset(cmd, 0, sizeof(*cmd));
  758. cmd->data[0] = devid;
  759. CMD_SET_TYPE(cmd, CMD_INV_IRT);
  760. }
  761. /*
  762. * Writes the command to the IOMMUs command buffer and informs the
  763. * hardware about the new command.
  764. */
  765. static int __iommu_queue_command_sync(struct amd_iommu *iommu,
  766. struct iommu_cmd *cmd,
  767. bool sync)
  768. {
  769. u32 left, tail, head, next_tail;
  770. again:
  771. head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  772. tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  773. next_tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
  774. left = (head - next_tail) % CMD_BUFFER_SIZE;
  775. if (left <= 2) {
  776. struct iommu_cmd sync_cmd;
  777. int ret;
  778. iommu->cmd_sem = 0;
  779. build_completion_wait(&sync_cmd, (u64)&iommu->cmd_sem);
  780. copy_cmd_to_buffer(iommu, &sync_cmd, tail);
  781. if ((ret = wait_on_sem(&iommu->cmd_sem)) != 0)
  782. return ret;
  783. goto again;
  784. }
  785. copy_cmd_to_buffer(iommu, cmd, tail);
  786. /* We need to sync now to make sure all commands are processed */
  787. iommu->need_sync = sync;
  788. return 0;
  789. }
  790. static int iommu_queue_command_sync(struct amd_iommu *iommu,
  791. struct iommu_cmd *cmd,
  792. bool sync)
  793. {
  794. unsigned long flags;
  795. int ret;
  796. spin_lock_irqsave(&iommu->lock, flags);
  797. ret = __iommu_queue_command_sync(iommu, cmd, sync);
  798. spin_unlock_irqrestore(&iommu->lock, flags);
  799. return ret;
  800. }
  801. static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  802. {
  803. return iommu_queue_command_sync(iommu, cmd, true);
  804. }
  805. /*
  806. * This function queues a completion wait command into the command
  807. * buffer of an IOMMU
  808. */
  809. static int iommu_completion_wait(struct amd_iommu *iommu)
  810. {
  811. struct iommu_cmd cmd;
  812. unsigned long flags;
  813. int ret;
  814. if (!iommu->need_sync)
  815. return 0;
  816. build_completion_wait(&cmd, (u64)&iommu->cmd_sem);
  817. spin_lock_irqsave(&iommu->lock, flags);
  818. iommu->cmd_sem = 0;
  819. ret = __iommu_queue_command_sync(iommu, &cmd, false);
  820. if (ret)
  821. goto out_unlock;
  822. ret = wait_on_sem(&iommu->cmd_sem);
  823. out_unlock:
  824. spin_unlock_irqrestore(&iommu->lock, flags);
  825. return ret;
  826. }
  827. static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
  828. {
  829. struct iommu_cmd cmd;
  830. build_inv_dte(&cmd, devid);
  831. return iommu_queue_command(iommu, &cmd);
  832. }
  833. static void iommu_flush_dte_all(struct amd_iommu *iommu)
  834. {
  835. u32 devid;
  836. for (devid = 0; devid <= 0xffff; ++devid)
  837. iommu_flush_dte(iommu, devid);
  838. iommu_completion_wait(iommu);
  839. }
  840. /*
  841. * This function uses heavy locking and may disable irqs for some time. But
  842. * this is no issue because it is only called during resume.
  843. */
  844. static void iommu_flush_tlb_all(struct amd_iommu *iommu)
  845. {
  846. u32 dom_id;
  847. for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
  848. struct iommu_cmd cmd;
  849. build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  850. dom_id, 1);
  851. iommu_queue_command(iommu, &cmd);
  852. }
  853. iommu_completion_wait(iommu);
  854. }
  855. static void iommu_flush_all(struct amd_iommu *iommu)
  856. {
  857. struct iommu_cmd cmd;
  858. build_inv_all(&cmd);
  859. iommu_queue_command(iommu, &cmd);
  860. iommu_completion_wait(iommu);
  861. }
  862. static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
  863. {
  864. struct iommu_cmd cmd;
  865. build_inv_irt(&cmd, devid);
  866. iommu_queue_command(iommu, &cmd);
  867. }
  868. static void iommu_flush_irt_all(struct amd_iommu *iommu)
  869. {
  870. u32 devid;
  871. for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
  872. iommu_flush_irt(iommu, devid);
  873. iommu_completion_wait(iommu);
  874. }
  875. void iommu_flush_all_caches(struct amd_iommu *iommu)
  876. {
  877. if (iommu_feature(iommu, FEATURE_IA)) {
  878. iommu_flush_all(iommu);
  879. } else {
  880. iommu_flush_dte_all(iommu);
  881. iommu_flush_irt_all(iommu);
  882. iommu_flush_tlb_all(iommu);
  883. }
  884. }
  885. /*
  886. * Command send function for flushing on-device TLB
  887. */
  888. static int device_flush_iotlb(struct iommu_dev_data *dev_data,
  889. u64 address, size_t size)
  890. {
  891. struct amd_iommu *iommu;
  892. struct iommu_cmd cmd;
  893. int qdep;
  894. qdep = dev_data->ats.qdep;
  895. iommu = amd_iommu_rlookup_table[dev_data->devid];
  896. build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
  897. return iommu_queue_command(iommu, &cmd);
  898. }
  899. /*
  900. * Command send function for invalidating a device table entry
  901. */
  902. static int device_flush_dte(struct iommu_dev_data *dev_data)
  903. {
  904. struct amd_iommu *iommu;
  905. u16 alias;
  906. int ret;
  907. iommu = amd_iommu_rlookup_table[dev_data->devid];
  908. alias = dev_data->alias;
  909. ret = iommu_flush_dte(iommu, dev_data->devid);
  910. if (!ret && alias != dev_data->devid)
  911. ret = iommu_flush_dte(iommu, alias);
  912. if (ret)
  913. return ret;
  914. if (dev_data->ats.enabled)
  915. ret = device_flush_iotlb(dev_data, 0, ~0UL);
  916. return ret;
  917. }
  918. /*
  919. * TLB invalidation function which is called from the mapping functions.
  920. * It invalidates a single PTE if the range to flush is within a single
  921. * page. Otherwise it flushes the whole TLB of the IOMMU.
  922. */
  923. static void __domain_flush_pages(struct protection_domain *domain,
  924. u64 address, size_t size, int pde)
  925. {
  926. struct iommu_dev_data *dev_data;
  927. struct iommu_cmd cmd;
  928. int ret = 0, i;
  929. build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
  930. for (i = 0; i < amd_iommus_present; ++i) {
  931. if (!domain->dev_iommu[i])
  932. continue;
  933. /*
  934. * Devices of this domain are behind this IOMMU
  935. * We need a TLB flush
  936. */
  937. ret |= iommu_queue_command(amd_iommus[i], &cmd);
  938. }
  939. list_for_each_entry(dev_data, &domain->dev_list, list) {
  940. if (!dev_data->ats.enabled)
  941. continue;
  942. ret |= device_flush_iotlb(dev_data, address, size);
  943. }
  944. WARN_ON(ret);
  945. }
  946. static void domain_flush_pages(struct protection_domain *domain,
  947. u64 address, size_t size)
  948. {
  949. __domain_flush_pages(domain, address, size, 0);
  950. }
  951. /* Flush the whole IO/TLB for a given protection domain */
  952. static void domain_flush_tlb(struct protection_domain *domain)
  953. {
  954. __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
  955. }
  956. /* Flush the whole IO/TLB for a given protection domain - including PDE */
  957. static void domain_flush_tlb_pde(struct protection_domain *domain)
  958. {
  959. __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
  960. }
  961. static void domain_flush_complete(struct protection_domain *domain)
  962. {
  963. int i;
  964. for (i = 0; i < amd_iommus_present; ++i) {
  965. if (domain && !domain->dev_iommu[i])
  966. continue;
  967. /*
  968. * Devices of this domain are behind this IOMMU
  969. * We need to wait for completion of all commands.
  970. */
  971. iommu_completion_wait(amd_iommus[i]);
  972. }
  973. }
  974. /*
  975. * This function flushes the DTEs for all devices in domain
  976. */
  977. static void domain_flush_devices(struct protection_domain *domain)
  978. {
  979. struct iommu_dev_data *dev_data;
  980. list_for_each_entry(dev_data, &domain->dev_list, list)
  981. device_flush_dte(dev_data);
  982. }
  983. /****************************************************************************
  984. *
  985. * The functions below are used the create the page table mappings for
  986. * unity mapped regions.
  987. *
  988. ****************************************************************************/
  989. /*
  990. * This function is used to add another level to an IO page table. Adding
  991. * another level increases the size of the address space by 9 bits to a size up
  992. * to 64 bits.
  993. */
  994. static bool increase_address_space(struct protection_domain *domain,
  995. gfp_t gfp)
  996. {
  997. u64 *pte;
  998. if (domain->mode == PAGE_MODE_6_LEVEL)
  999. /* address space already 64 bit large */
  1000. return false;
  1001. pte = (void *)get_zeroed_page(gfp);
  1002. if (!pte)
  1003. return false;
  1004. *pte = PM_LEVEL_PDE(domain->mode,
  1005. virt_to_phys(domain->pt_root));
  1006. domain->pt_root = pte;
  1007. domain->mode += 1;
  1008. domain->updated = true;
  1009. return true;
  1010. }
  1011. static u64 *alloc_pte(struct protection_domain *domain,
  1012. unsigned long address,
  1013. unsigned long page_size,
  1014. u64 **pte_page,
  1015. gfp_t gfp)
  1016. {
  1017. int level, end_lvl;
  1018. u64 *pte, *page;
  1019. BUG_ON(!is_power_of_2(page_size));
  1020. while (address > PM_LEVEL_SIZE(domain->mode))
  1021. increase_address_space(domain, gfp);
  1022. level = domain->mode - 1;
  1023. pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
  1024. address = PAGE_SIZE_ALIGN(address, page_size);
  1025. end_lvl = PAGE_SIZE_LEVEL(page_size);
  1026. while (level > end_lvl) {
  1027. u64 __pte, __npte;
  1028. __pte = *pte;
  1029. if (!IOMMU_PTE_PRESENT(__pte)) {
  1030. page = (u64 *)get_zeroed_page(gfp);
  1031. if (!page)
  1032. return NULL;
  1033. __npte = PM_LEVEL_PDE(level, virt_to_phys(page));
  1034. if (cmpxchg64(pte, __pte, __npte)) {
  1035. free_page((unsigned long)page);
  1036. continue;
  1037. }
  1038. }
  1039. /* No level skipping support yet */
  1040. if (PM_PTE_LEVEL(*pte) != level)
  1041. return NULL;
  1042. level -= 1;
  1043. pte = IOMMU_PTE_PAGE(*pte);
  1044. if (pte_page && level == end_lvl)
  1045. *pte_page = pte;
  1046. pte = &pte[PM_LEVEL_INDEX(level, address)];
  1047. }
  1048. return pte;
  1049. }
  1050. /*
  1051. * This function checks if there is a PTE for a given dma address. If
  1052. * there is one, it returns the pointer to it.
  1053. */
  1054. static u64 *fetch_pte(struct protection_domain *domain,
  1055. unsigned long address,
  1056. unsigned long *page_size)
  1057. {
  1058. int level;
  1059. u64 *pte;
  1060. if (address > PM_LEVEL_SIZE(domain->mode))
  1061. return NULL;
  1062. level = domain->mode - 1;
  1063. pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
  1064. *page_size = PTE_LEVEL_PAGE_SIZE(level);
  1065. while (level > 0) {
  1066. /* Not Present */
  1067. if (!IOMMU_PTE_PRESENT(*pte))
  1068. return NULL;
  1069. /* Large PTE */
  1070. if (PM_PTE_LEVEL(*pte) == 7 ||
  1071. PM_PTE_LEVEL(*pte) == 0)
  1072. break;
  1073. /* No level skipping support yet */
  1074. if (PM_PTE_LEVEL(*pte) != level)
  1075. return NULL;
  1076. level -= 1;
  1077. /* Walk to the next level */
  1078. pte = IOMMU_PTE_PAGE(*pte);
  1079. pte = &pte[PM_LEVEL_INDEX(level, address)];
  1080. *page_size = PTE_LEVEL_PAGE_SIZE(level);
  1081. }
  1082. if (PM_PTE_LEVEL(*pte) == 0x07) {
  1083. unsigned long pte_mask;
  1084. /*
  1085. * If we have a series of large PTEs, make
  1086. * sure to return a pointer to the first one.
  1087. */
  1088. *page_size = pte_mask = PTE_PAGE_SIZE(*pte);
  1089. pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
  1090. pte = (u64 *)(((unsigned long)pte) & pte_mask);
  1091. }
  1092. return pte;
  1093. }
  1094. /*
  1095. * Generic mapping functions. It maps a physical address into a DMA
  1096. * address space. It allocates the page table pages if necessary.
  1097. * In the future it can be extended to a generic mapping function
  1098. * supporting all features of AMD IOMMU page tables like level skipping
  1099. * and full 64 bit address spaces.
  1100. */
  1101. static int iommu_map_page(struct protection_domain *dom,
  1102. unsigned long bus_addr,
  1103. unsigned long phys_addr,
  1104. unsigned long page_size,
  1105. int prot,
  1106. gfp_t gfp)
  1107. {
  1108. u64 __pte, *pte;
  1109. int i, count;
  1110. BUG_ON(!IS_ALIGNED(bus_addr, page_size));
  1111. BUG_ON(!IS_ALIGNED(phys_addr, page_size));
  1112. if (!(prot & IOMMU_PROT_MASK))
  1113. return -EINVAL;
  1114. count = PAGE_SIZE_PTE_COUNT(page_size);
  1115. pte = alloc_pte(dom, bus_addr, page_size, NULL, gfp);
  1116. if (!pte)
  1117. return -ENOMEM;
  1118. for (i = 0; i < count; ++i)
  1119. if (IOMMU_PTE_PRESENT(pte[i]))
  1120. return -EBUSY;
  1121. if (count > 1) {
  1122. __pte = PAGE_SIZE_PTE(phys_addr, page_size);
  1123. __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
  1124. } else
  1125. __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
  1126. if (prot & IOMMU_PROT_IR)
  1127. __pte |= IOMMU_PTE_IR;
  1128. if (prot & IOMMU_PROT_IW)
  1129. __pte |= IOMMU_PTE_IW;
  1130. for (i = 0; i < count; ++i)
  1131. pte[i] = __pte;
  1132. update_domain(dom);
  1133. return 0;
  1134. }
  1135. static unsigned long iommu_unmap_page(struct protection_domain *dom,
  1136. unsigned long bus_addr,
  1137. unsigned long page_size)
  1138. {
  1139. unsigned long long unmapped;
  1140. unsigned long unmap_size;
  1141. u64 *pte;
  1142. BUG_ON(!is_power_of_2(page_size));
  1143. unmapped = 0;
  1144. while (unmapped < page_size) {
  1145. pte = fetch_pte(dom, bus_addr, &unmap_size);
  1146. if (pte) {
  1147. int i, count;
  1148. count = PAGE_SIZE_PTE_COUNT(unmap_size);
  1149. for (i = 0; i < count; i++)
  1150. pte[i] = 0ULL;
  1151. }
  1152. bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
  1153. unmapped += unmap_size;
  1154. }
  1155. BUG_ON(unmapped && !is_power_of_2(unmapped));
  1156. return unmapped;
  1157. }
  1158. /****************************************************************************
  1159. *
  1160. * The next functions belong to the address allocator for the dma_ops
  1161. * interface functions.
  1162. *
  1163. ****************************************************************************/
  1164. static unsigned long dma_ops_alloc_iova(struct device *dev,
  1165. struct dma_ops_domain *dma_dom,
  1166. unsigned int pages, u64 dma_mask)
  1167. {
  1168. unsigned long pfn = 0;
  1169. pages = __roundup_pow_of_two(pages);
  1170. if (dma_mask > DMA_BIT_MASK(32))
  1171. pfn = alloc_iova_fast(&dma_dom->iovad, pages,
  1172. IOVA_PFN(DMA_BIT_MASK(32)));
  1173. if (!pfn)
  1174. pfn = alloc_iova_fast(&dma_dom->iovad, pages, IOVA_PFN(dma_mask));
  1175. return (pfn << PAGE_SHIFT);
  1176. }
  1177. static void dma_ops_free_iova(struct dma_ops_domain *dma_dom,
  1178. unsigned long address,
  1179. unsigned int pages)
  1180. {
  1181. pages = __roundup_pow_of_two(pages);
  1182. address >>= PAGE_SHIFT;
  1183. free_iova_fast(&dma_dom->iovad, address, pages);
  1184. }
  1185. /****************************************************************************
  1186. *
  1187. * The next functions belong to the domain allocation. A domain is
  1188. * allocated for every IOMMU as the default domain. If device isolation
  1189. * is enabled, every device get its own domain. The most important thing
  1190. * about domains is the page table mapping the DMA address space they
  1191. * contain.
  1192. *
  1193. ****************************************************************************/
  1194. /*
  1195. * This function adds a protection domain to the global protection domain list
  1196. */
  1197. static void add_domain_to_list(struct protection_domain *domain)
  1198. {
  1199. unsigned long flags;
  1200. spin_lock_irqsave(&amd_iommu_pd_lock, flags);
  1201. list_add(&domain->list, &amd_iommu_pd_list);
  1202. spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
  1203. }
  1204. /*
  1205. * This function removes a protection domain to the global
  1206. * protection domain list
  1207. */
  1208. static void del_domain_from_list(struct protection_domain *domain)
  1209. {
  1210. unsigned long flags;
  1211. spin_lock_irqsave(&amd_iommu_pd_lock, flags);
  1212. list_del(&domain->list);
  1213. spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
  1214. }
  1215. static u16 domain_id_alloc(void)
  1216. {
  1217. unsigned long flags;
  1218. int id;
  1219. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1220. id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
  1221. BUG_ON(id == 0);
  1222. if (id > 0 && id < MAX_DOMAIN_ID)
  1223. __set_bit(id, amd_iommu_pd_alloc_bitmap);
  1224. else
  1225. id = 0;
  1226. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1227. return id;
  1228. }
  1229. static void domain_id_free(int id)
  1230. {
  1231. unsigned long flags;
  1232. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1233. if (id > 0 && id < MAX_DOMAIN_ID)
  1234. __clear_bit(id, amd_iommu_pd_alloc_bitmap);
  1235. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1236. }
  1237. #define DEFINE_FREE_PT_FN(LVL, FN) \
  1238. static void free_pt_##LVL (unsigned long __pt) \
  1239. { \
  1240. unsigned long p; \
  1241. u64 *pt; \
  1242. int i; \
  1243. \
  1244. pt = (u64 *)__pt; \
  1245. \
  1246. for (i = 0; i < 512; ++i) { \
  1247. /* PTE present? */ \
  1248. if (!IOMMU_PTE_PRESENT(pt[i])) \
  1249. continue; \
  1250. \
  1251. /* Large PTE? */ \
  1252. if (PM_PTE_LEVEL(pt[i]) == 0 || \
  1253. PM_PTE_LEVEL(pt[i]) == 7) \
  1254. continue; \
  1255. \
  1256. p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
  1257. FN(p); \
  1258. } \
  1259. free_page((unsigned long)pt); \
  1260. }
  1261. DEFINE_FREE_PT_FN(l2, free_page)
  1262. DEFINE_FREE_PT_FN(l3, free_pt_l2)
  1263. DEFINE_FREE_PT_FN(l4, free_pt_l3)
  1264. DEFINE_FREE_PT_FN(l5, free_pt_l4)
  1265. DEFINE_FREE_PT_FN(l6, free_pt_l5)
  1266. static void free_pagetable(struct protection_domain *domain)
  1267. {
  1268. unsigned long root = (unsigned long)domain->pt_root;
  1269. switch (domain->mode) {
  1270. case PAGE_MODE_NONE:
  1271. break;
  1272. case PAGE_MODE_1_LEVEL:
  1273. free_page(root);
  1274. break;
  1275. case PAGE_MODE_2_LEVEL:
  1276. free_pt_l2(root);
  1277. break;
  1278. case PAGE_MODE_3_LEVEL:
  1279. free_pt_l3(root);
  1280. break;
  1281. case PAGE_MODE_4_LEVEL:
  1282. free_pt_l4(root);
  1283. break;
  1284. case PAGE_MODE_5_LEVEL:
  1285. free_pt_l5(root);
  1286. break;
  1287. case PAGE_MODE_6_LEVEL:
  1288. free_pt_l6(root);
  1289. break;
  1290. default:
  1291. BUG();
  1292. }
  1293. }
  1294. static void free_gcr3_tbl_level1(u64 *tbl)
  1295. {
  1296. u64 *ptr;
  1297. int i;
  1298. for (i = 0; i < 512; ++i) {
  1299. if (!(tbl[i] & GCR3_VALID))
  1300. continue;
  1301. ptr = __va(tbl[i] & PAGE_MASK);
  1302. free_page((unsigned long)ptr);
  1303. }
  1304. }
  1305. static void free_gcr3_tbl_level2(u64 *tbl)
  1306. {
  1307. u64 *ptr;
  1308. int i;
  1309. for (i = 0; i < 512; ++i) {
  1310. if (!(tbl[i] & GCR3_VALID))
  1311. continue;
  1312. ptr = __va(tbl[i] & PAGE_MASK);
  1313. free_gcr3_tbl_level1(ptr);
  1314. }
  1315. }
  1316. static void free_gcr3_table(struct protection_domain *domain)
  1317. {
  1318. if (domain->glx == 2)
  1319. free_gcr3_tbl_level2(domain->gcr3_tbl);
  1320. else if (domain->glx == 1)
  1321. free_gcr3_tbl_level1(domain->gcr3_tbl);
  1322. else
  1323. BUG_ON(domain->glx != 0);
  1324. free_page((unsigned long)domain->gcr3_tbl);
  1325. }
  1326. /*
  1327. * Free a domain, only used if something went wrong in the
  1328. * allocation path and we need to free an already allocated page table
  1329. */
  1330. static void dma_ops_domain_free(struct dma_ops_domain *dom)
  1331. {
  1332. if (!dom)
  1333. return;
  1334. del_domain_from_list(&dom->domain);
  1335. put_iova_domain(&dom->iovad);
  1336. free_pagetable(&dom->domain);
  1337. kfree(dom);
  1338. }
  1339. /*
  1340. * Allocates a new protection domain usable for the dma_ops functions.
  1341. * It also initializes the page table and the address allocator data
  1342. * structures required for the dma_ops interface
  1343. */
  1344. static struct dma_ops_domain *dma_ops_domain_alloc(void)
  1345. {
  1346. struct dma_ops_domain *dma_dom;
  1347. dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
  1348. if (!dma_dom)
  1349. return NULL;
  1350. if (protection_domain_init(&dma_dom->domain))
  1351. goto free_dma_dom;
  1352. dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
  1353. dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  1354. dma_dom->domain.flags = PD_DMA_OPS_MASK;
  1355. if (!dma_dom->domain.pt_root)
  1356. goto free_dma_dom;
  1357. init_iova_domain(&dma_dom->iovad, PAGE_SIZE,
  1358. IOVA_START_PFN, DMA_32BIT_PFN);
  1359. /* Initialize reserved ranges */
  1360. copy_reserved_iova(&reserved_iova_ranges, &dma_dom->iovad);
  1361. add_domain_to_list(&dma_dom->domain);
  1362. return dma_dom;
  1363. free_dma_dom:
  1364. dma_ops_domain_free(dma_dom);
  1365. return NULL;
  1366. }
  1367. /*
  1368. * little helper function to check whether a given protection domain is a
  1369. * dma_ops domain
  1370. */
  1371. static bool dma_ops_domain(struct protection_domain *domain)
  1372. {
  1373. return domain->flags & PD_DMA_OPS_MASK;
  1374. }
  1375. static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
  1376. {
  1377. u64 pte_root = 0;
  1378. u64 flags = 0;
  1379. if (domain->mode != PAGE_MODE_NONE)
  1380. pte_root = virt_to_phys(domain->pt_root);
  1381. pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
  1382. << DEV_ENTRY_MODE_SHIFT;
  1383. pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
  1384. flags = amd_iommu_dev_table[devid].data[1];
  1385. if (ats)
  1386. flags |= DTE_FLAG_IOTLB;
  1387. if (domain->flags & PD_IOMMUV2_MASK) {
  1388. u64 gcr3 = __pa(domain->gcr3_tbl);
  1389. u64 glx = domain->glx;
  1390. u64 tmp;
  1391. pte_root |= DTE_FLAG_GV;
  1392. pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
  1393. /* First mask out possible old values for GCR3 table */
  1394. tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
  1395. flags &= ~tmp;
  1396. tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
  1397. flags &= ~tmp;
  1398. /* Encode GCR3 table into DTE */
  1399. tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
  1400. pte_root |= tmp;
  1401. tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
  1402. flags |= tmp;
  1403. tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
  1404. flags |= tmp;
  1405. }
  1406. flags &= ~(0xffffUL);
  1407. flags |= domain->id;
  1408. amd_iommu_dev_table[devid].data[1] = flags;
  1409. amd_iommu_dev_table[devid].data[0] = pte_root;
  1410. }
  1411. static void clear_dte_entry(u16 devid)
  1412. {
  1413. /* remove entry from the device table seen by the hardware */
  1414. amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
  1415. amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
  1416. amd_iommu_apply_erratum_63(devid);
  1417. }
  1418. static void do_attach(struct iommu_dev_data *dev_data,
  1419. struct protection_domain *domain)
  1420. {
  1421. struct amd_iommu *iommu;
  1422. u16 alias;
  1423. bool ats;
  1424. iommu = amd_iommu_rlookup_table[dev_data->devid];
  1425. alias = dev_data->alias;
  1426. ats = dev_data->ats.enabled;
  1427. /* Update data structures */
  1428. dev_data->domain = domain;
  1429. list_add(&dev_data->list, &domain->dev_list);
  1430. /* Do reference counting */
  1431. domain->dev_iommu[iommu->index] += 1;
  1432. domain->dev_cnt += 1;
  1433. /* Update device table */
  1434. set_dte_entry(dev_data->devid, domain, ats);
  1435. if (alias != dev_data->devid)
  1436. set_dte_entry(alias, domain, ats);
  1437. device_flush_dte(dev_data);
  1438. }
  1439. static void do_detach(struct iommu_dev_data *dev_data)
  1440. {
  1441. struct amd_iommu *iommu;
  1442. u16 alias;
  1443. /*
  1444. * First check if the device is still attached. It might already
  1445. * be detached from its domain because the generic
  1446. * iommu_detach_group code detached it and we try again here in
  1447. * our alias handling.
  1448. */
  1449. if (!dev_data->domain)
  1450. return;
  1451. iommu = amd_iommu_rlookup_table[dev_data->devid];
  1452. alias = dev_data->alias;
  1453. /* decrease reference counters */
  1454. dev_data->domain->dev_iommu[iommu->index] -= 1;
  1455. dev_data->domain->dev_cnt -= 1;
  1456. /* Update data structures */
  1457. dev_data->domain = NULL;
  1458. list_del(&dev_data->list);
  1459. clear_dte_entry(dev_data->devid);
  1460. if (alias != dev_data->devid)
  1461. clear_dte_entry(alias);
  1462. /* Flush the DTE entry */
  1463. device_flush_dte(dev_data);
  1464. }
  1465. /*
  1466. * If a device is not yet associated with a domain, this function does
  1467. * assigns it visible for the hardware
  1468. */
  1469. static int __attach_device(struct iommu_dev_data *dev_data,
  1470. struct protection_domain *domain)
  1471. {
  1472. int ret;
  1473. /*
  1474. * Must be called with IRQs disabled. Warn here to detect early
  1475. * when its not.
  1476. */
  1477. WARN_ON(!irqs_disabled());
  1478. /* lock domain */
  1479. spin_lock(&domain->lock);
  1480. ret = -EBUSY;
  1481. if (dev_data->domain != NULL)
  1482. goto out_unlock;
  1483. /* Attach alias group root */
  1484. do_attach(dev_data, domain);
  1485. ret = 0;
  1486. out_unlock:
  1487. /* ready */
  1488. spin_unlock(&domain->lock);
  1489. return ret;
  1490. }
  1491. static void pdev_iommuv2_disable(struct pci_dev *pdev)
  1492. {
  1493. pci_disable_ats(pdev);
  1494. pci_disable_pri(pdev);
  1495. pci_disable_pasid(pdev);
  1496. }
  1497. /* FIXME: Change generic reset-function to do the same */
  1498. static int pri_reset_while_enabled(struct pci_dev *pdev)
  1499. {
  1500. u16 control;
  1501. int pos;
  1502. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  1503. if (!pos)
  1504. return -EINVAL;
  1505. pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
  1506. control |= PCI_PRI_CTRL_RESET;
  1507. pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
  1508. return 0;
  1509. }
  1510. static int pdev_iommuv2_enable(struct pci_dev *pdev)
  1511. {
  1512. bool reset_enable;
  1513. int reqs, ret;
  1514. /* FIXME: Hardcode number of outstanding requests for now */
  1515. reqs = 32;
  1516. if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
  1517. reqs = 1;
  1518. reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
  1519. /* Only allow access to user-accessible pages */
  1520. ret = pci_enable_pasid(pdev, 0);
  1521. if (ret)
  1522. goto out_err;
  1523. /* First reset the PRI state of the device */
  1524. ret = pci_reset_pri(pdev);
  1525. if (ret)
  1526. goto out_err;
  1527. /* Enable PRI */
  1528. ret = pci_enable_pri(pdev, reqs);
  1529. if (ret)
  1530. goto out_err;
  1531. if (reset_enable) {
  1532. ret = pri_reset_while_enabled(pdev);
  1533. if (ret)
  1534. goto out_err;
  1535. }
  1536. ret = pci_enable_ats(pdev, PAGE_SHIFT);
  1537. if (ret)
  1538. goto out_err;
  1539. return 0;
  1540. out_err:
  1541. pci_disable_pri(pdev);
  1542. pci_disable_pasid(pdev);
  1543. return ret;
  1544. }
  1545. /* FIXME: Move this to PCI code */
  1546. #define PCI_PRI_TLP_OFF (1 << 15)
  1547. static bool pci_pri_tlp_required(struct pci_dev *pdev)
  1548. {
  1549. u16 status;
  1550. int pos;
  1551. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  1552. if (!pos)
  1553. return false;
  1554. pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
  1555. return (status & PCI_PRI_TLP_OFF) ? true : false;
  1556. }
  1557. /*
  1558. * If a device is not yet associated with a domain, this function
  1559. * assigns it visible for the hardware
  1560. */
  1561. static int attach_device(struct device *dev,
  1562. struct protection_domain *domain)
  1563. {
  1564. struct pci_dev *pdev;
  1565. struct iommu_dev_data *dev_data;
  1566. unsigned long flags;
  1567. int ret;
  1568. dev_data = get_dev_data(dev);
  1569. if (!dev_is_pci(dev))
  1570. goto skip_ats_check;
  1571. pdev = to_pci_dev(dev);
  1572. if (domain->flags & PD_IOMMUV2_MASK) {
  1573. if (!dev_data->passthrough)
  1574. return -EINVAL;
  1575. if (dev_data->iommu_v2) {
  1576. if (pdev_iommuv2_enable(pdev) != 0)
  1577. return -EINVAL;
  1578. dev_data->ats.enabled = true;
  1579. dev_data->ats.qdep = pci_ats_queue_depth(pdev);
  1580. dev_data->pri_tlp = pci_pri_tlp_required(pdev);
  1581. }
  1582. } else if (amd_iommu_iotlb_sup &&
  1583. pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
  1584. dev_data->ats.enabled = true;
  1585. dev_data->ats.qdep = pci_ats_queue_depth(pdev);
  1586. }
  1587. skip_ats_check:
  1588. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1589. ret = __attach_device(dev_data, domain);
  1590. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1591. /*
  1592. * We might boot into a crash-kernel here. The crashed kernel
  1593. * left the caches in the IOMMU dirty. So we have to flush
  1594. * here to evict all dirty stuff.
  1595. */
  1596. domain_flush_tlb_pde(domain);
  1597. return ret;
  1598. }
  1599. /*
  1600. * Removes a device from a protection domain (unlocked)
  1601. */
  1602. static void __detach_device(struct iommu_dev_data *dev_data)
  1603. {
  1604. struct protection_domain *domain;
  1605. /*
  1606. * Must be called with IRQs disabled. Warn here to detect early
  1607. * when its not.
  1608. */
  1609. WARN_ON(!irqs_disabled());
  1610. if (WARN_ON(!dev_data->domain))
  1611. return;
  1612. domain = dev_data->domain;
  1613. spin_lock(&domain->lock);
  1614. do_detach(dev_data);
  1615. spin_unlock(&domain->lock);
  1616. }
  1617. /*
  1618. * Removes a device from a protection domain (with devtable_lock held)
  1619. */
  1620. static void detach_device(struct device *dev)
  1621. {
  1622. struct protection_domain *domain;
  1623. struct iommu_dev_data *dev_data;
  1624. unsigned long flags;
  1625. dev_data = get_dev_data(dev);
  1626. domain = dev_data->domain;
  1627. /* lock device table */
  1628. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1629. __detach_device(dev_data);
  1630. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1631. if (!dev_is_pci(dev))
  1632. return;
  1633. if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
  1634. pdev_iommuv2_disable(to_pci_dev(dev));
  1635. else if (dev_data->ats.enabled)
  1636. pci_disable_ats(to_pci_dev(dev));
  1637. dev_data->ats.enabled = false;
  1638. }
  1639. static int amd_iommu_add_device(struct device *dev)
  1640. {
  1641. struct iommu_dev_data *dev_data;
  1642. struct iommu_domain *domain;
  1643. struct amd_iommu *iommu;
  1644. int ret, devid;
  1645. if (!check_device(dev) || get_dev_data(dev))
  1646. return 0;
  1647. devid = get_device_id(dev);
  1648. if (devid < 0)
  1649. return devid;
  1650. iommu = amd_iommu_rlookup_table[devid];
  1651. ret = iommu_init_device(dev);
  1652. if (ret) {
  1653. if (ret != -ENOTSUPP)
  1654. pr_err("Failed to initialize device %s - trying to proceed anyway\n",
  1655. dev_name(dev));
  1656. iommu_ignore_device(dev);
  1657. dev->archdata.dma_ops = &nommu_dma_ops;
  1658. goto out;
  1659. }
  1660. init_iommu_group(dev);
  1661. dev_data = get_dev_data(dev);
  1662. BUG_ON(!dev_data);
  1663. if (iommu_pass_through || dev_data->iommu_v2)
  1664. iommu_request_dm_for_dev(dev);
  1665. /* Domains are initialized for this device - have a look what we ended up with */
  1666. domain = iommu_get_domain_for_dev(dev);
  1667. if (domain->type == IOMMU_DOMAIN_IDENTITY)
  1668. dev_data->passthrough = true;
  1669. else
  1670. dev->archdata.dma_ops = &amd_iommu_dma_ops;
  1671. out:
  1672. iommu_completion_wait(iommu);
  1673. return 0;
  1674. }
  1675. static void amd_iommu_remove_device(struct device *dev)
  1676. {
  1677. struct amd_iommu *iommu;
  1678. int devid;
  1679. if (!check_device(dev))
  1680. return;
  1681. devid = get_device_id(dev);
  1682. if (devid < 0)
  1683. return;
  1684. iommu = amd_iommu_rlookup_table[devid];
  1685. iommu_uninit_device(dev);
  1686. iommu_completion_wait(iommu);
  1687. }
  1688. static struct iommu_group *amd_iommu_device_group(struct device *dev)
  1689. {
  1690. if (dev_is_pci(dev))
  1691. return pci_device_group(dev);
  1692. return acpihid_device_group(dev);
  1693. }
  1694. /*****************************************************************************
  1695. *
  1696. * The next functions belong to the dma_ops mapping/unmapping code.
  1697. *
  1698. *****************************************************************************/
  1699. static void __queue_flush(struct flush_queue *queue)
  1700. {
  1701. struct protection_domain *domain;
  1702. unsigned long flags;
  1703. int idx;
  1704. /* First flush TLB of all known domains */
  1705. spin_lock_irqsave(&amd_iommu_pd_lock, flags);
  1706. list_for_each_entry(domain, &amd_iommu_pd_list, list)
  1707. domain_flush_tlb(domain);
  1708. spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
  1709. /* Wait until flushes have completed */
  1710. domain_flush_complete(NULL);
  1711. for (idx = 0; idx < queue->next; ++idx) {
  1712. struct flush_queue_entry *entry;
  1713. entry = queue->entries + idx;
  1714. free_iova_fast(&entry->dma_dom->iovad,
  1715. entry->iova_pfn,
  1716. entry->pages);
  1717. /* Not really necessary, just to make sure we catch any bugs */
  1718. entry->dma_dom = NULL;
  1719. }
  1720. queue->next = 0;
  1721. }
  1722. static void queue_flush_all(void)
  1723. {
  1724. int cpu;
  1725. for_each_possible_cpu(cpu) {
  1726. struct flush_queue *queue;
  1727. unsigned long flags;
  1728. queue = per_cpu_ptr(&flush_queue, cpu);
  1729. spin_lock_irqsave(&queue->lock, flags);
  1730. if (queue->next > 0)
  1731. __queue_flush(queue);
  1732. spin_unlock_irqrestore(&queue->lock, flags);
  1733. }
  1734. }
  1735. static void queue_flush_timeout(unsigned long unsused)
  1736. {
  1737. atomic_set(&queue_timer_on, 0);
  1738. queue_flush_all();
  1739. }
  1740. static void queue_add(struct dma_ops_domain *dma_dom,
  1741. unsigned long address, unsigned long pages)
  1742. {
  1743. struct flush_queue_entry *entry;
  1744. struct flush_queue *queue;
  1745. unsigned long flags;
  1746. int idx;
  1747. pages = __roundup_pow_of_two(pages);
  1748. address >>= PAGE_SHIFT;
  1749. queue = get_cpu_ptr(&flush_queue);
  1750. spin_lock_irqsave(&queue->lock, flags);
  1751. if (queue->next == FLUSH_QUEUE_SIZE)
  1752. __queue_flush(queue);
  1753. idx = queue->next++;
  1754. entry = queue->entries + idx;
  1755. entry->iova_pfn = address;
  1756. entry->pages = pages;
  1757. entry->dma_dom = dma_dom;
  1758. spin_unlock_irqrestore(&queue->lock, flags);
  1759. if (atomic_cmpxchg(&queue_timer_on, 0, 1) == 0)
  1760. mod_timer(&queue_timer, jiffies + msecs_to_jiffies(10));
  1761. put_cpu_ptr(&flush_queue);
  1762. }
  1763. /*
  1764. * In the dma_ops path we only have the struct device. This function
  1765. * finds the corresponding IOMMU, the protection domain and the
  1766. * requestor id for a given device.
  1767. * If the device is not yet associated with a domain this is also done
  1768. * in this function.
  1769. */
  1770. static struct protection_domain *get_domain(struct device *dev)
  1771. {
  1772. struct protection_domain *domain;
  1773. if (!check_device(dev))
  1774. return ERR_PTR(-EINVAL);
  1775. domain = get_dev_data(dev)->domain;
  1776. if (!dma_ops_domain(domain))
  1777. return ERR_PTR(-EBUSY);
  1778. return domain;
  1779. }
  1780. static void update_device_table(struct protection_domain *domain)
  1781. {
  1782. struct iommu_dev_data *dev_data;
  1783. list_for_each_entry(dev_data, &domain->dev_list, list) {
  1784. set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
  1785. if (dev_data->devid == dev_data->alias)
  1786. continue;
  1787. /* There is an alias, update device table entry for it */
  1788. set_dte_entry(dev_data->alias, domain, dev_data->ats.enabled);
  1789. }
  1790. }
  1791. static void update_domain(struct protection_domain *domain)
  1792. {
  1793. if (!domain->updated)
  1794. return;
  1795. update_device_table(domain);
  1796. domain_flush_devices(domain);
  1797. domain_flush_tlb_pde(domain);
  1798. domain->updated = false;
  1799. }
  1800. static int dir2prot(enum dma_data_direction direction)
  1801. {
  1802. if (direction == DMA_TO_DEVICE)
  1803. return IOMMU_PROT_IR;
  1804. else if (direction == DMA_FROM_DEVICE)
  1805. return IOMMU_PROT_IW;
  1806. else if (direction == DMA_BIDIRECTIONAL)
  1807. return IOMMU_PROT_IW | IOMMU_PROT_IR;
  1808. else
  1809. return 0;
  1810. }
  1811. /*
  1812. * This function contains common code for mapping of a physically
  1813. * contiguous memory region into DMA address space. It is used by all
  1814. * mapping functions provided with this IOMMU driver.
  1815. * Must be called with the domain lock held.
  1816. */
  1817. static dma_addr_t __map_single(struct device *dev,
  1818. struct dma_ops_domain *dma_dom,
  1819. phys_addr_t paddr,
  1820. size_t size,
  1821. enum dma_data_direction direction,
  1822. u64 dma_mask)
  1823. {
  1824. dma_addr_t offset = paddr & ~PAGE_MASK;
  1825. dma_addr_t address, start, ret;
  1826. unsigned int pages;
  1827. int prot = 0;
  1828. int i;
  1829. pages = iommu_num_pages(paddr, size, PAGE_SIZE);
  1830. paddr &= PAGE_MASK;
  1831. address = dma_ops_alloc_iova(dev, dma_dom, pages, dma_mask);
  1832. if (address == DMA_ERROR_CODE)
  1833. goto out;
  1834. prot = dir2prot(direction);
  1835. start = address;
  1836. for (i = 0; i < pages; ++i) {
  1837. ret = iommu_map_page(&dma_dom->domain, start, paddr,
  1838. PAGE_SIZE, prot, GFP_ATOMIC);
  1839. if (ret)
  1840. goto out_unmap;
  1841. paddr += PAGE_SIZE;
  1842. start += PAGE_SIZE;
  1843. }
  1844. address += offset;
  1845. if (unlikely(amd_iommu_np_cache)) {
  1846. domain_flush_pages(&dma_dom->domain, address, size);
  1847. domain_flush_complete(&dma_dom->domain);
  1848. }
  1849. out:
  1850. return address;
  1851. out_unmap:
  1852. for (--i; i >= 0; --i) {
  1853. start -= PAGE_SIZE;
  1854. iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
  1855. }
  1856. domain_flush_tlb(&dma_dom->domain);
  1857. domain_flush_complete(&dma_dom->domain);
  1858. dma_ops_free_iova(dma_dom, address, pages);
  1859. return DMA_ERROR_CODE;
  1860. }
  1861. /*
  1862. * Does the reverse of the __map_single function. Must be called with
  1863. * the domain lock held too
  1864. */
  1865. static void __unmap_single(struct dma_ops_domain *dma_dom,
  1866. dma_addr_t dma_addr,
  1867. size_t size,
  1868. int dir)
  1869. {
  1870. dma_addr_t flush_addr;
  1871. dma_addr_t i, start;
  1872. unsigned int pages;
  1873. flush_addr = dma_addr;
  1874. pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  1875. dma_addr &= PAGE_MASK;
  1876. start = dma_addr;
  1877. for (i = 0; i < pages; ++i) {
  1878. iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
  1879. start += PAGE_SIZE;
  1880. }
  1881. if (amd_iommu_unmap_flush) {
  1882. dma_ops_free_iova(dma_dom, dma_addr, pages);
  1883. domain_flush_tlb(&dma_dom->domain);
  1884. domain_flush_complete(&dma_dom->domain);
  1885. } else {
  1886. queue_add(dma_dom, dma_addr, pages);
  1887. }
  1888. }
  1889. /*
  1890. * The exported map_single function for dma_ops.
  1891. */
  1892. static dma_addr_t map_page(struct device *dev, struct page *page,
  1893. unsigned long offset, size_t size,
  1894. enum dma_data_direction dir,
  1895. unsigned long attrs)
  1896. {
  1897. phys_addr_t paddr = page_to_phys(page) + offset;
  1898. struct protection_domain *domain;
  1899. struct dma_ops_domain *dma_dom;
  1900. u64 dma_mask;
  1901. domain = get_domain(dev);
  1902. if (PTR_ERR(domain) == -EINVAL)
  1903. return (dma_addr_t)paddr;
  1904. else if (IS_ERR(domain))
  1905. return DMA_ERROR_CODE;
  1906. dma_mask = *dev->dma_mask;
  1907. dma_dom = to_dma_ops_domain(domain);
  1908. return __map_single(dev, dma_dom, paddr, size, dir, dma_mask);
  1909. }
  1910. /*
  1911. * The exported unmap_single function for dma_ops.
  1912. */
  1913. static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
  1914. enum dma_data_direction dir, unsigned long attrs)
  1915. {
  1916. struct protection_domain *domain;
  1917. struct dma_ops_domain *dma_dom;
  1918. domain = get_domain(dev);
  1919. if (IS_ERR(domain))
  1920. return;
  1921. dma_dom = to_dma_ops_domain(domain);
  1922. __unmap_single(dma_dom, dma_addr, size, dir);
  1923. }
  1924. static int sg_num_pages(struct device *dev,
  1925. struct scatterlist *sglist,
  1926. int nelems)
  1927. {
  1928. unsigned long mask, boundary_size;
  1929. struct scatterlist *s;
  1930. int i, npages = 0;
  1931. mask = dma_get_seg_boundary(dev);
  1932. boundary_size = mask + 1 ? ALIGN(mask + 1, PAGE_SIZE) >> PAGE_SHIFT :
  1933. 1UL << (BITS_PER_LONG - PAGE_SHIFT);
  1934. for_each_sg(sglist, s, nelems, i) {
  1935. int p, n;
  1936. s->dma_address = npages << PAGE_SHIFT;
  1937. p = npages % boundary_size;
  1938. n = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
  1939. if (p + n > boundary_size)
  1940. npages += boundary_size - p;
  1941. npages += n;
  1942. }
  1943. return npages;
  1944. }
  1945. /*
  1946. * The exported map_sg function for dma_ops (handles scatter-gather
  1947. * lists).
  1948. */
  1949. static int map_sg(struct device *dev, struct scatterlist *sglist,
  1950. int nelems, enum dma_data_direction direction,
  1951. unsigned long attrs)
  1952. {
  1953. int mapped_pages = 0, npages = 0, prot = 0, i;
  1954. struct protection_domain *domain;
  1955. struct dma_ops_domain *dma_dom;
  1956. struct scatterlist *s;
  1957. unsigned long address;
  1958. u64 dma_mask;
  1959. domain = get_domain(dev);
  1960. if (IS_ERR(domain))
  1961. return 0;
  1962. dma_dom = to_dma_ops_domain(domain);
  1963. dma_mask = *dev->dma_mask;
  1964. npages = sg_num_pages(dev, sglist, nelems);
  1965. address = dma_ops_alloc_iova(dev, dma_dom, npages, dma_mask);
  1966. if (address == DMA_ERROR_CODE)
  1967. goto out_err;
  1968. prot = dir2prot(direction);
  1969. /* Map all sg entries */
  1970. for_each_sg(sglist, s, nelems, i) {
  1971. int j, pages = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
  1972. for (j = 0; j < pages; ++j) {
  1973. unsigned long bus_addr, phys_addr;
  1974. int ret;
  1975. bus_addr = address + s->dma_address + (j << PAGE_SHIFT);
  1976. phys_addr = (sg_phys(s) & PAGE_MASK) + (j << PAGE_SHIFT);
  1977. ret = iommu_map_page(domain, bus_addr, phys_addr, PAGE_SIZE, prot, GFP_ATOMIC);
  1978. if (ret)
  1979. goto out_unmap;
  1980. mapped_pages += 1;
  1981. }
  1982. }
  1983. /* Everything is mapped - write the right values into s->dma_address */
  1984. for_each_sg(sglist, s, nelems, i) {
  1985. s->dma_address += address + s->offset;
  1986. s->dma_length = s->length;
  1987. }
  1988. return nelems;
  1989. out_unmap:
  1990. pr_err("%s: IOMMU mapping error in map_sg (io-pages: %d)\n",
  1991. dev_name(dev), npages);
  1992. for_each_sg(sglist, s, nelems, i) {
  1993. int j, pages = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
  1994. for (j = 0; j < pages; ++j) {
  1995. unsigned long bus_addr;
  1996. bus_addr = address + s->dma_address + (j << PAGE_SHIFT);
  1997. iommu_unmap_page(domain, bus_addr, PAGE_SIZE);
  1998. if (--mapped_pages)
  1999. goto out_free_iova;
  2000. }
  2001. }
  2002. out_free_iova:
  2003. free_iova_fast(&dma_dom->iovad, address, npages);
  2004. out_err:
  2005. return 0;
  2006. }
  2007. /*
  2008. * The exported map_sg function for dma_ops (handles scatter-gather
  2009. * lists).
  2010. */
  2011. static void unmap_sg(struct device *dev, struct scatterlist *sglist,
  2012. int nelems, enum dma_data_direction dir,
  2013. unsigned long attrs)
  2014. {
  2015. struct protection_domain *domain;
  2016. struct dma_ops_domain *dma_dom;
  2017. unsigned long startaddr;
  2018. int npages = 2;
  2019. domain = get_domain(dev);
  2020. if (IS_ERR(domain))
  2021. return;
  2022. startaddr = sg_dma_address(sglist) & PAGE_MASK;
  2023. dma_dom = to_dma_ops_domain(domain);
  2024. npages = sg_num_pages(dev, sglist, nelems);
  2025. __unmap_single(dma_dom, startaddr, npages << PAGE_SHIFT, dir);
  2026. }
  2027. /*
  2028. * The exported alloc_coherent function for dma_ops.
  2029. */
  2030. static void *alloc_coherent(struct device *dev, size_t size,
  2031. dma_addr_t *dma_addr, gfp_t flag,
  2032. unsigned long attrs)
  2033. {
  2034. u64 dma_mask = dev->coherent_dma_mask;
  2035. struct protection_domain *domain;
  2036. struct dma_ops_domain *dma_dom;
  2037. struct page *page;
  2038. domain = get_domain(dev);
  2039. if (PTR_ERR(domain) == -EINVAL) {
  2040. page = alloc_pages(flag, get_order(size));
  2041. *dma_addr = page_to_phys(page);
  2042. return page_address(page);
  2043. } else if (IS_ERR(domain))
  2044. return NULL;
  2045. dma_dom = to_dma_ops_domain(domain);
  2046. size = PAGE_ALIGN(size);
  2047. dma_mask = dev->coherent_dma_mask;
  2048. flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  2049. flag |= __GFP_ZERO;
  2050. page = alloc_pages(flag | __GFP_NOWARN, get_order(size));
  2051. if (!page) {
  2052. if (!gfpflags_allow_blocking(flag))
  2053. return NULL;
  2054. page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
  2055. get_order(size));
  2056. if (!page)
  2057. return NULL;
  2058. }
  2059. if (!dma_mask)
  2060. dma_mask = *dev->dma_mask;
  2061. *dma_addr = __map_single(dev, dma_dom, page_to_phys(page),
  2062. size, DMA_BIDIRECTIONAL, dma_mask);
  2063. if (*dma_addr == DMA_ERROR_CODE)
  2064. goto out_free;
  2065. return page_address(page);
  2066. out_free:
  2067. if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
  2068. __free_pages(page, get_order(size));
  2069. return NULL;
  2070. }
  2071. /*
  2072. * The exported free_coherent function for dma_ops.
  2073. */
  2074. static void free_coherent(struct device *dev, size_t size,
  2075. void *virt_addr, dma_addr_t dma_addr,
  2076. unsigned long attrs)
  2077. {
  2078. struct protection_domain *domain;
  2079. struct dma_ops_domain *dma_dom;
  2080. struct page *page;
  2081. page = virt_to_page(virt_addr);
  2082. size = PAGE_ALIGN(size);
  2083. domain = get_domain(dev);
  2084. if (IS_ERR(domain))
  2085. goto free_mem;
  2086. dma_dom = to_dma_ops_domain(domain);
  2087. __unmap_single(dma_dom, dma_addr, size, DMA_BIDIRECTIONAL);
  2088. free_mem:
  2089. if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
  2090. __free_pages(page, get_order(size));
  2091. }
  2092. /*
  2093. * This function is called by the DMA layer to find out if we can handle a
  2094. * particular device. It is part of the dma_ops.
  2095. */
  2096. static int amd_iommu_dma_supported(struct device *dev, u64 mask)
  2097. {
  2098. return check_device(dev);
  2099. }
  2100. static struct dma_map_ops amd_iommu_dma_ops = {
  2101. .alloc = alloc_coherent,
  2102. .free = free_coherent,
  2103. .map_page = map_page,
  2104. .unmap_page = unmap_page,
  2105. .map_sg = map_sg,
  2106. .unmap_sg = unmap_sg,
  2107. .dma_supported = amd_iommu_dma_supported,
  2108. };
  2109. static int init_reserved_iova_ranges(void)
  2110. {
  2111. struct pci_dev *pdev = NULL;
  2112. struct iova *val;
  2113. init_iova_domain(&reserved_iova_ranges, PAGE_SIZE,
  2114. IOVA_START_PFN, DMA_32BIT_PFN);
  2115. lockdep_set_class(&reserved_iova_ranges.iova_rbtree_lock,
  2116. &reserved_rbtree_key);
  2117. /* MSI memory range */
  2118. val = reserve_iova(&reserved_iova_ranges,
  2119. IOVA_PFN(MSI_RANGE_START), IOVA_PFN(MSI_RANGE_END));
  2120. if (!val) {
  2121. pr_err("Reserving MSI range failed\n");
  2122. return -ENOMEM;
  2123. }
  2124. /* HT memory range */
  2125. val = reserve_iova(&reserved_iova_ranges,
  2126. IOVA_PFN(HT_RANGE_START), IOVA_PFN(HT_RANGE_END));
  2127. if (!val) {
  2128. pr_err("Reserving HT range failed\n");
  2129. return -ENOMEM;
  2130. }
  2131. /*
  2132. * Memory used for PCI resources
  2133. * FIXME: Check whether we can reserve the PCI-hole completly
  2134. */
  2135. for_each_pci_dev(pdev) {
  2136. int i;
  2137. for (i = 0; i < PCI_NUM_RESOURCES; ++i) {
  2138. struct resource *r = &pdev->resource[i];
  2139. if (!(r->flags & IORESOURCE_MEM))
  2140. continue;
  2141. val = reserve_iova(&reserved_iova_ranges,
  2142. IOVA_PFN(r->start),
  2143. IOVA_PFN(r->end));
  2144. if (!val) {
  2145. pr_err("Reserve pci-resource range failed\n");
  2146. return -ENOMEM;
  2147. }
  2148. }
  2149. }
  2150. return 0;
  2151. }
  2152. int __init amd_iommu_init_api(void)
  2153. {
  2154. int ret, cpu, err = 0;
  2155. ret = iova_cache_get();
  2156. if (ret)
  2157. return ret;
  2158. ret = init_reserved_iova_ranges();
  2159. if (ret)
  2160. return ret;
  2161. for_each_possible_cpu(cpu) {
  2162. struct flush_queue *queue = per_cpu_ptr(&flush_queue, cpu);
  2163. queue->entries = kzalloc(FLUSH_QUEUE_SIZE *
  2164. sizeof(*queue->entries),
  2165. GFP_KERNEL);
  2166. if (!queue->entries)
  2167. goto out_put_iova;
  2168. spin_lock_init(&queue->lock);
  2169. }
  2170. err = bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
  2171. if (err)
  2172. return err;
  2173. #ifdef CONFIG_ARM_AMBA
  2174. err = bus_set_iommu(&amba_bustype, &amd_iommu_ops);
  2175. if (err)
  2176. return err;
  2177. #endif
  2178. err = bus_set_iommu(&platform_bus_type, &amd_iommu_ops);
  2179. if (err)
  2180. return err;
  2181. return 0;
  2182. out_put_iova:
  2183. for_each_possible_cpu(cpu) {
  2184. struct flush_queue *queue = per_cpu_ptr(&flush_queue, cpu);
  2185. kfree(queue->entries);
  2186. }
  2187. return -ENOMEM;
  2188. }
  2189. int __init amd_iommu_init_dma_ops(void)
  2190. {
  2191. setup_timer(&queue_timer, queue_flush_timeout, 0);
  2192. atomic_set(&queue_timer_on, 0);
  2193. swiotlb = iommu_pass_through ? 1 : 0;
  2194. iommu_detected = 1;
  2195. /*
  2196. * In case we don't initialize SWIOTLB (actually the common case
  2197. * when AMD IOMMU is enabled), make sure there are global
  2198. * dma_ops set as a fall-back for devices not handled by this
  2199. * driver (for example non-PCI devices).
  2200. */
  2201. if (!swiotlb)
  2202. dma_ops = &nommu_dma_ops;
  2203. if (amd_iommu_unmap_flush)
  2204. pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
  2205. else
  2206. pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
  2207. return 0;
  2208. }
  2209. /*****************************************************************************
  2210. *
  2211. * The following functions belong to the exported interface of AMD IOMMU
  2212. *
  2213. * This interface allows access to lower level functions of the IOMMU
  2214. * like protection domain handling and assignement of devices to domains
  2215. * which is not possible with the dma_ops interface.
  2216. *
  2217. *****************************************************************************/
  2218. static void cleanup_domain(struct protection_domain *domain)
  2219. {
  2220. struct iommu_dev_data *entry;
  2221. unsigned long flags;
  2222. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  2223. while (!list_empty(&domain->dev_list)) {
  2224. entry = list_first_entry(&domain->dev_list,
  2225. struct iommu_dev_data, list);
  2226. __detach_device(entry);
  2227. }
  2228. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  2229. }
  2230. static void protection_domain_free(struct protection_domain *domain)
  2231. {
  2232. if (!domain)
  2233. return;
  2234. del_domain_from_list(domain);
  2235. if (domain->id)
  2236. domain_id_free(domain->id);
  2237. kfree(domain);
  2238. }
  2239. static int protection_domain_init(struct protection_domain *domain)
  2240. {
  2241. spin_lock_init(&domain->lock);
  2242. mutex_init(&domain->api_lock);
  2243. domain->id = domain_id_alloc();
  2244. if (!domain->id)
  2245. return -ENOMEM;
  2246. INIT_LIST_HEAD(&domain->dev_list);
  2247. return 0;
  2248. }
  2249. static struct protection_domain *protection_domain_alloc(void)
  2250. {
  2251. struct protection_domain *domain;
  2252. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  2253. if (!domain)
  2254. return NULL;
  2255. if (protection_domain_init(domain))
  2256. goto out_err;
  2257. add_domain_to_list(domain);
  2258. return domain;
  2259. out_err:
  2260. kfree(domain);
  2261. return NULL;
  2262. }
  2263. static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
  2264. {
  2265. struct protection_domain *pdomain;
  2266. struct dma_ops_domain *dma_domain;
  2267. switch (type) {
  2268. case IOMMU_DOMAIN_UNMANAGED:
  2269. pdomain = protection_domain_alloc();
  2270. if (!pdomain)
  2271. return NULL;
  2272. pdomain->mode = PAGE_MODE_3_LEVEL;
  2273. pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  2274. if (!pdomain->pt_root) {
  2275. protection_domain_free(pdomain);
  2276. return NULL;
  2277. }
  2278. pdomain->domain.geometry.aperture_start = 0;
  2279. pdomain->domain.geometry.aperture_end = ~0ULL;
  2280. pdomain->domain.geometry.force_aperture = true;
  2281. break;
  2282. case IOMMU_DOMAIN_DMA:
  2283. dma_domain = dma_ops_domain_alloc();
  2284. if (!dma_domain) {
  2285. pr_err("AMD-Vi: Failed to allocate\n");
  2286. return NULL;
  2287. }
  2288. pdomain = &dma_domain->domain;
  2289. break;
  2290. case IOMMU_DOMAIN_IDENTITY:
  2291. pdomain = protection_domain_alloc();
  2292. if (!pdomain)
  2293. return NULL;
  2294. pdomain->mode = PAGE_MODE_NONE;
  2295. break;
  2296. default:
  2297. return NULL;
  2298. }
  2299. return &pdomain->domain;
  2300. }
  2301. static void amd_iommu_domain_free(struct iommu_domain *dom)
  2302. {
  2303. struct protection_domain *domain;
  2304. struct dma_ops_domain *dma_dom;
  2305. domain = to_pdomain(dom);
  2306. if (domain->dev_cnt > 0)
  2307. cleanup_domain(domain);
  2308. BUG_ON(domain->dev_cnt != 0);
  2309. if (!dom)
  2310. return;
  2311. switch (dom->type) {
  2312. case IOMMU_DOMAIN_DMA:
  2313. /*
  2314. * First make sure the domain is no longer referenced from the
  2315. * flush queue
  2316. */
  2317. queue_flush_all();
  2318. /* Now release the domain */
  2319. dma_dom = to_dma_ops_domain(domain);
  2320. dma_ops_domain_free(dma_dom);
  2321. break;
  2322. default:
  2323. if (domain->mode != PAGE_MODE_NONE)
  2324. free_pagetable(domain);
  2325. if (domain->flags & PD_IOMMUV2_MASK)
  2326. free_gcr3_table(domain);
  2327. protection_domain_free(domain);
  2328. break;
  2329. }
  2330. }
  2331. static void amd_iommu_detach_device(struct iommu_domain *dom,
  2332. struct device *dev)
  2333. {
  2334. struct iommu_dev_data *dev_data = dev->archdata.iommu;
  2335. struct amd_iommu *iommu;
  2336. int devid;
  2337. if (!check_device(dev))
  2338. return;
  2339. devid = get_device_id(dev);
  2340. if (devid < 0)
  2341. return;
  2342. if (dev_data->domain != NULL)
  2343. detach_device(dev);
  2344. iommu = amd_iommu_rlookup_table[devid];
  2345. if (!iommu)
  2346. return;
  2347. iommu_completion_wait(iommu);
  2348. }
  2349. static int amd_iommu_attach_device(struct iommu_domain *dom,
  2350. struct device *dev)
  2351. {
  2352. struct protection_domain *domain = to_pdomain(dom);
  2353. struct iommu_dev_data *dev_data;
  2354. struct amd_iommu *iommu;
  2355. int ret;
  2356. if (!check_device(dev))
  2357. return -EINVAL;
  2358. dev_data = dev->archdata.iommu;
  2359. iommu = amd_iommu_rlookup_table[dev_data->devid];
  2360. if (!iommu)
  2361. return -EINVAL;
  2362. if (dev_data->domain)
  2363. detach_device(dev);
  2364. ret = attach_device(dev, domain);
  2365. iommu_completion_wait(iommu);
  2366. return ret;
  2367. }
  2368. static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
  2369. phys_addr_t paddr, size_t page_size, int iommu_prot)
  2370. {
  2371. struct protection_domain *domain = to_pdomain(dom);
  2372. int prot = 0;
  2373. int ret;
  2374. if (domain->mode == PAGE_MODE_NONE)
  2375. return -EINVAL;
  2376. if (iommu_prot & IOMMU_READ)
  2377. prot |= IOMMU_PROT_IR;
  2378. if (iommu_prot & IOMMU_WRITE)
  2379. prot |= IOMMU_PROT_IW;
  2380. mutex_lock(&domain->api_lock);
  2381. ret = iommu_map_page(domain, iova, paddr, page_size, prot, GFP_KERNEL);
  2382. mutex_unlock(&domain->api_lock);
  2383. return ret;
  2384. }
  2385. static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
  2386. size_t page_size)
  2387. {
  2388. struct protection_domain *domain = to_pdomain(dom);
  2389. size_t unmap_size;
  2390. if (domain->mode == PAGE_MODE_NONE)
  2391. return -EINVAL;
  2392. mutex_lock(&domain->api_lock);
  2393. unmap_size = iommu_unmap_page(domain, iova, page_size);
  2394. mutex_unlock(&domain->api_lock);
  2395. domain_flush_tlb_pde(domain);
  2396. return unmap_size;
  2397. }
  2398. static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
  2399. dma_addr_t iova)
  2400. {
  2401. struct protection_domain *domain = to_pdomain(dom);
  2402. unsigned long offset_mask, pte_pgsize;
  2403. u64 *pte, __pte;
  2404. if (domain->mode == PAGE_MODE_NONE)
  2405. return iova;
  2406. pte = fetch_pte(domain, iova, &pte_pgsize);
  2407. if (!pte || !IOMMU_PTE_PRESENT(*pte))
  2408. return 0;
  2409. offset_mask = pte_pgsize - 1;
  2410. __pte = *pte & PM_ADDR_MASK;
  2411. return (__pte & ~offset_mask) | (iova & offset_mask);
  2412. }
  2413. static bool amd_iommu_capable(enum iommu_cap cap)
  2414. {
  2415. switch (cap) {
  2416. case IOMMU_CAP_CACHE_COHERENCY:
  2417. return true;
  2418. case IOMMU_CAP_INTR_REMAP:
  2419. return (irq_remapping_enabled == 1);
  2420. case IOMMU_CAP_NOEXEC:
  2421. return false;
  2422. }
  2423. return false;
  2424. }
  2425. static void amd_iommu_get_dm_regions(struct device *dev,
  2426. struct list_head *head)
  2427. {
  2428. struct unity_map_entry *entry;
  2429. int devid;
  2430. devid = get_device_id(dev);
  2431. if (devid < 0)
  2432. return;
  2433. list_for_each_entry(entry, &amd_iommu_unity_map, list) {
  2434. struct iommu_dm_region *region;
  2435. if (devid < entry->devid_start || devid > entry->devid_end)
  2436. continue;
  2437. region = kzalloc(sizeof(*region), GFP_KERNEL);
  2438. if (!region) {
  2439. pr_err("Out of memory allocating dm-regions for %s\n",
  2440. dev_name(dev));
  2441. return;
  2442. }
  2443. region->start = entry->address_start;
  2444. region->length = entry->address_end - entry->address_start;
  2445. if (entry->prot & IOMMU_PROT_IR)
  2446. region->prot |= IOMMU_READ;
  2447. if (entry->prot & IOMMU_PROT_IW)
  2448. region->prot |= IOMMU_WRITE;
  2449. list_add_tail(&region->list, head);
  2450. }
  2451. }
  2452. static void amd_iommu_put_dm_regions(struct device *dev,
  2453. struct list_head *head)
  2454. {
  2455. struct iommu_dm_region *entry, *next;
  2456. list_for_each_entry_safe(entry, next, head, list)
  2457. kfree(entry);
  2458. }
  2459. static void amd_iommu_apply_dm_region(struct device *dev,
  2460. struct iommu_domain *domain,
  2461. struct iommu_dm_region *region)
  2462. {
  2463. struct dma_ops_domain *dma_dom = to_dma_ops_domain(to_pdomain(domain));
  2464. unsigned long start, end;
  2465. start = IOVA_PFN(region->start);
  2466. end = IOVA_PFN(region->start + region->length);
  2467. WARN_ON_ONCE(reserve_iova(&dma_dom->iovad, start, end) == NULL);
  2468. }
  2469. static const struct iommu_ops amd_iommu_ops = {
  2470. .capable = amd_iommu_capable,
  2471. .domain_alloc = amd_iommu_domain_alloc,
  2472. .domain_free = amd_iommu_domain_free,
  2473. .attach_dev = amd_iommu_attach_device,
  2474. .detach_dev = amd_iommu_detach_device,
  2475. .map = amd_iommu_map,
  2476. .unmap = amd_iommu_unmap,
  2477. .map_sg = default_iommu_map_sg,
  2478. .iova_to_phys = amd_iommu_iova_to_phys,
  2479. .add_device = amd_iommu_add_device,
  2480. .remove_device = amd_iommu_remove_device,
  2481. .device_group = amd_iommu_device_group,
  2482. .get_dm_regions = amd_iommu_get_dm_regions,
  2483. .put_dm_regions = amd_iommu_put_dm_regions,
  2484. .apply_dm_region = amd_iommu_apply_dm_region,
  2485. .pgsize_bitmap = AMD_IOMMU_PGSIZES,
  2486. };
  2487. /*****************************************************************************
  2488. *
  2489. * The next functions do a basic initialization of IOMMU for pass through
  2490. * mode
  2491. *
  2492. * In passthrough mode the IOMMU is initialized and enabled but not used for
  2493. * DMA-API translation.
  2494. *
  2495. *****************************************************************************/
  2496. /* IOMMUv2 specific functions */
  2497. int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
  2498. {
  2499. return atomic_notifier_chain_register(&ppr_notifier, nb);
  2500. }
  2501. EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
  2502. int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
  2503. {
  2504. return atomic_notifier_chain_unregister(&ppr_notifier, nb);
  2505. }
  2506. EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
  2507. void amd_iommu_domain_direct_map(struct iommu_domain *dom)
  2508. {
  2509. struct protection_domain *domain = to_pdomain(dom);
  2510. unsigned long flags;
  2511. spin_lock_irqsave(&domain->lock, flags);
  2512. /* Update data structure */
  2513. domain->mode = PAGE_MODE_NONE;
  2514. domain->updated = true;
  2515. /* Make changes visible to IOMMUs */
  2516. update_domain(domain);
  2517. /* Page-table is not visible to IOMMU anymore, so free it */
  2518. free_pagetable(domain);
  2519. spin_unlock_irqrestore(&domain->lock, flags);
  2520. }
  2521. EXPORT_SYMBOL(amd_iommu_domain_direct_map);
  2522. int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
  2523. {
  2524. struct protection_domain *domain = to_pdomain(dom);
  2525. unsigned long flags;
  2526. int levels, ret;
  2527. if (pasids <= 0 || pasids > (PASID_MASK + 1))
  2528. return -EINVAL;
  2529. /* Number of GCR3 table levels required */
  2530. for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
  2531. levels += 1;
  2532. if (levels > amd_iommu_max_glx_val)
  2533. return -EINVAL;
  2534. spin_lock_irqsave(&domain->lock, flags);
  2535. /*
  2536. * Save us all sanity checks whether devices already in the
  2537. * domain support IOMMUv2. Just force that the domain has no
  2538. * devices attached when it is switched into IOMMUv2 mode.
  2539. */
  2540. ret = -EBUSY;
  2541. if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
  2542. goto out;
  2543. ret = -ENOMEM;
  2544. domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
  2545. if (domain->gcr3_tbl == NULL)
  2546. goto out;
  2547. domain->glx = levels;
  2548. domain->flags |= PD_IOMMUV2_MASK;
  2549. domain->updated = true;
  2550. update_domain(domain);
  2551. ret = 0;
  2552. out:
  2553. spin_unlock_irqrestore(&domain->lock, flags);
  2554. return ret;
  2555. }
  2556. EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
  2557. static int __flush_pasid(struct protection_domain *domain, int pasid,
  2558. u64 address, bool size)
  2559. {
  2560. struct iommu_dev_data *dev_data;
  2561. struct iommu_cmd cmd;
  2562. int i, ret;
  2563. if (!(domain->flags & PD_IOMMUV2_MASK))
  2564. return -EINVAL;
  2565. build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
  2566. /*
  2567. * IOMMU TLB needs to be flushed before Device TLB to
  2568. * prevent device TLB refill from IOMMU TLB
  2569. */
  2570. for (i = 0; i < amd_iommus_present; ++i) {
  2571. if (domain->dev_iommu[i] == 0)
  2572. continue;
  2573. ret = iommu_queue_command(amd_iommus[i], &cmd);
  2574. if (ret != 0)
  2575. goto out;
  2576. }
  2577. /* Wait until IOMMU TLB flushes are complete */
  2578. domain_flush_complete(domain);
  2579. /* Now flush device TLBs */
  2580. list_for_each_entry(dev_data, &domain->dev_list, list) {
  2581. struct amd_iommu *iommu;
  2582. int qdep;
  2583. /*
  2584. There might be non-IOMMUv2 capable devices in an IOMMUv2
  2585. * domain.
  2586. */
  2587. if (!dev_data->ats.enabled)
  2588. continue;
  2589. qdep = dev_data->ats.qdep;
  2590. iommu = amd_iommu_rlookup_table[dev_data->devid];
  2591. build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
  2592. qdep, address, size);
  2593. ret = iommu_queue_command(iommu, &cmd);
  2594. if (ret != 0)
  2595. goto out;
  2596. }
  2597. /* Wait until all device TLBs are flushed */
  2598. domain_flush_complete(domain);
  2599. ret = 0;
  2600. out:
  2601. return ret;
  2602. }
  2603. static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
  2604. u64 address)
  2605. {
  2606. return __flush_pasid(domain, pasid, address, false);
  2607. }
  2608. int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
  2609. u64 address)
  2610. {
  2611. struct protection_domain *domain = to_pdomain(dom);
  2612. unsigned long flags;
  2613. int ret;
  2614. spin_lock_irqsave(&domain->lock, flags);
  2615. ret = __amd_iommu_flush_page(domain, pasid, address);
  2616. spin_unlock_irqrestore(&domain->lock, flags);
  2617. return ret;
  2618. }
  2619. EXPORT_SYMBOL(amd_iommu_flush_page);
  2620. static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
  2621. {
  2622. return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  2623. true);
  2624. }
  2625. int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
  2626. {
  2627. struct protection_domain *domain = to_pdomain(dom);
  2628. unsigned long flags;
  2629. int ret;
  2630. spin_lock_irqsave(&domain->lock, flags);
  2631. ret = __amd_iommu_flush_tlb(domain, pasid);
  2632. spin_unlock_irqrestore(&domain->lock, flags);
  2633. return ret;
  2634. }
  2635. EXPORT_SYMBOL(amd_iommu_flush_tlb);
  2636. static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
  2637. {
  2638. int index;
  2639. u64 *pte;
  2640. while (true) {
  2641. index = (pasid >> (9 * level)) & 0x1ff;
  2642. pte = &root[index];
  2643. if (level == 0)
  2644. break;
  2645. if (!(*pte & GCR3_VALID)) {
  2646. if (!alloc)
  2647. return NULL;
  2648. root = (void *)get_zeroed_page(GFP_ATOMIC);
  2649. if (root == NULL)
  2650. return NULL;
  2651. *pte = __pa(root) | GCR3_VALID;
  2652. }
  2653. root = __va(*pte & PAGE_MASK);
  2654. level -= 1;
  2655. }
  2656. return pte;
  2657. }
  2658. static int __set_gcr3(struct protection_domain *domain, int pasid,
  2659. unsigned long cr3)
  2660. {
  2661. u64 *pte;
  2662. if (domain->mode != PAGE_MODE_NONE)
  2663. return -EINVAL;
  2664. pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
  2665. if (pte == NULL)
  2666. return -ENOMEM;
  2667. *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
  2668. return __amd_iommu_flush_tlb(domain, pasid);
  2669. }
  2670. static int __clear_gcr3(struct protection_domain *domain, int pasid)
  2671. {
  2672. u64 *pte;
  2673. if (domain->mode != PAGE_MODE_NONE)
  2674. return -EINVAL;
  2675. pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
  2676. if (pte == NULL)
  2677. return 0;
  2678. *pte = 0;
  2679. return __amd_iommu_flush_tlb(domain, pasid);
  2680. }
  2681. int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
  2682. unsigned long cr3)
  2683. {
  2684. struct protection_domain *domain = to_pdomain(dom);
  2685. unsigned long flags;
  2686. int ret;
  2687. spin_lock_irqsave(&domain->lock, flags);
  2688. ret = __set_gcr3(domain, pasid, cr3);
  2689. spin_unlock_irqrestore(&domain->lock, flags);
  2690. return ret;
  2691. }
  2692. EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
  2693. int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
  2694. {
  2695. struct protection_domain *domain = to_pdomain(dom);
  2696. unsigned long flags;
  2697. int ret;
  2698. spin_lock_irqsave(&domain->lock, flags);
  2699. ret = __clear_gcr3(domain, pasid);
  2700. spin_unlock_irqrestore(&domain->lock, flags);
  2701. return ret;
  2702. }
  2703. EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
  2704. int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
  2705. int status, int tag)
  2706. {
  2707. struct iommu_dev_data *dev_data;
  2708. struct amd_iommu *iommu;
  2709. struct iommu_cmd cmd;
  2710. dev_data = get_dev_data(&pdev->dev);
  2711. iommu = amd_iommu_rlookup_table[dev_data->devid];
  2712. build_complete_ppr(&cmd, dev_data->devid, pasid, status,
  2713. tag, dev_data->pri_tlp);
  2714. return iommu_queue_command(iommu, &cmd);
  2715. }
  2716. EXPORT_SYMBOL(amd_iommu_complete_ppr);
  2717. struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
  2718. {
  2719. struct protection_domain *pdomain;
  2720. pdomain = get_domain(&pdev->dev);
  2721. if (IS_ERR(pdomain))
  2722. return NULL;
  2723. /* Only return IOMMUv2 domains */
  2724. if (!(pdomain->flags & PD_IOMMUV2_MASK))
  2725. return NULL;
  2726. return &pdomain->domain;
  2727. }
  2728. EXPORT_SYMBOL(amd_iommu_get_v2_domain);
  2729. void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
  2730. {
  2731. struct iommu_dev_data *dev_data;
  2732. if (!amd_iommu_v2_supported())
  2733. return;
  2734. dev_data = get_dev_data(&pdev->dev);
  2735. dev_data->errata |= (1 << erratum);
  2736. }
  2737. EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
  2738. int amd_iommu_device_info(struct pci_dev *pdev,
  2739. struct amd_iommu_device_info *info)
  2740. {
  2741. int max_pasids;
  2742. int pos;
  2743. if (pdev == NULL || info == NULL)
  2744. return -EINVAL;
  2745. if (!amd_iommu_v2_supported())
  2746. return -EINVAL;
  2747. memset(info, 0, sizeof(*info));
  2748. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
  2749. if (pos)
  2750. info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
  2751. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  2752. if (pos)
  2753. info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
  2754. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  2755. if (pos) {
  2756. int features;
  2757. max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
  2758. max_pasids = min(max_pasids, (1 << 20));
  2759. info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
  2760. info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
  2761. features = pci_pasid_features(pdev);
  2762. if (features & PCI_PASID_CAP_EXEC)
  2763. info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
  2764. if (features & PCI_PASID_CAP_PRIV)
  2765. info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
  2766. }
  2767. return 0;
  2768. }
  2769. EXPORT_SYMBOL(amd_iommu_device_info);
  2770. #ifdef CONFIG_IRQ_REMAP
  2771. /*****************************************************************************
  2772. *
  2773. * Interrupt Remapping Implementation
  2774. *
  2775. *****************************************************************************/
  2776. union irte {
  2777. u32 val;
  2778. struct {
  2779. u32 valid : 1,
  2780. no_fault : 1,
  2781. int_type : 3,
  2782. rq_eoi : 1,
  2783. dm : 1,
  2784. rsvd_1 : 1,
  2785. destination : 8,
  2786. vector : 8,
  2787. rsvd_2 : 8;
  2788. } fields;
  2789. };
  2790. struct irq_2_irte {
  2791. u16 devid; /* Device ID for IRTE table */
  2792. u16 index; /* Index into IRTE table*/
  2793. };
  2794. struct amd_ir_data {
  2795. struct irq_2_irte irq_2_irte;
  2796. union irte irte_entry;
  2797. union {
  2798. struct msi_msg msi_entry;
  2799. };
  2800. };
  2801. static struct irq_chip amd_ir_chip;
  2802. #define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
  2803. #define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
  2804. #define DTE_IRQ_TABLE_LEN (8ULL << 1)
  2805. #define DTE_IRQ_REMAP_ENABLE 1ULL
  2806. static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
  2807. {
  2808. u64 dte;
  2809. dte = amd_iommu_dev_table[devid].data[2];
  2810. dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
  2811. dte |= virt_to_phys(table->table);
  2812. dte |= DTE_IRQ_REMAP_INTCTL;
  2813. dte |= DTE_IRQ_TABLE_LEN;
  2814. dte |= DTE_IRQ_REMAP_ENABLE;
  2815. amd_iommu_dev_table[devid].data[2] = dte;
  2816. }
  2817. #define IRTE_ALLOCATED (~1U)
  2818. static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
  2819. {
  2820. struct irq_remap_table *table = NULL;
  2821. struct amd_iommu *iommu;
  2822. unsigned long flags;
  2823. u16 alias;
  2824. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  2825. iommu = amd_iommu_rlookup_table[devid];
  2826. if (!iommu)
  2827. goto out_unlock;
  2828. table = irq_lookup_table[devid];
  2829. if (table)
  2830. goto out;
  2831. alias = amd_iommu_alias_table[devid];
  2832. table = irq_lookup_table[alias];
  2833. if (table) {
  2834. irq_lookup_table[devid] = table;
  2835. set_dte_irq_entry(devid, table);
  2836. iommu_flush_dte(iommu, devid);
  2837. goto out;
  2838. }
  2839. /* Nothing there yet, allocate new irq remapping table */
  2840. table = kzalloc(sizeof(*table), GFP_ATOMIC);
  2841. if (!table)
  2842. goto out;
  2843. /* Initialize table spin-lock */
  2844. spin_lock_init(&table->lock);
  2845. if (ioapic)
  2846. /* Keep the first 32 indexes free for IOAPIC interrupts */
  2847. table->min_index = 32;
  2848. table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
  2849. if (!table->table) {
  2850. kfree(table);
  2851. table = NULL;
  2852. goto out;
  2853. }
  2854. memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
  2855. if (ioapic) {
  2856. int i;
  2857. for (i = 0; i < 32; ++i)
  2858. table->table[i] = IRTE_ALLOCATED;
  2859. }
  2860. irq_lookup_table[devid] = table;
  2861. set_dte_irq_entry(devid, table);
  2862. iommu_flush_dte(iommu, devid);
  2863. if (devid != alias) {
  2864. irq_lookup_table[alias] = table;
  2865. set_dte_irq_entry(alias, table);
  2866. iommu_flush_dte(iommu, alias);
  2867. }
  2868. out:
  2869. iommu_completion_wait(iommu);
  2870. out_unlock:
  2871. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  2872. return table;
  2873. }
  2874. static int alloc_irq_index(u16 devid, int count)
  2875. {
  2876. struct irq_remap_table *table;
  2877. unsigned long flags;
  2878. int index, c;
  2879. table = get_irq_table(devid, false);
  2880. if (!table)
  2881. return -ENODEV;
  2882. spin_lock_irqsave(&table->lock, flags);
  2883. /* Scan table for free entries */
  2884. for (c = 0, index = table->min_index;
  2885. index < MAX_IRQS_PER_TABLE;
  2886. ++index) {
  2887. if (table->table[index] == 0)
  2888. c += 1;
  2889. else
  2890. c = 0;
  2891. if (c == count) {
  2892. for (; c != 0; --c)
  2893. table->table[index - c + 1] = IRTE_ALLOCATED;
  2894. index -= count - 1;
  2895. goto out;
  2896. }
  2897. }
  2898. index = -ENOSPC;
  2899. out:
  2900. spin_unlock_irqrestore(&table->lock, flags);
  2901. return index;
  2902. }
  2903. static int modify_irte(u16 devid, int index, union irte irte)
  2904. {
  2905. struct irq_remap_table *table;
  2906. struct amd_iommu *iommu;
  2907. unsigned long flags;
  2908. iommu = amd_iommu_rlookup_table[devid];
  2909. if (iommu == NULL)
  2910. return -EINVAL;
  2911. table = get_irq_table(devid, false);
  2912. if (!table)
  2913. return -ENOMEM;
  2914. spin_lock_irqsave(&table->lock, flags);
  2915. table->table[index] = irte.val;
  2916. spin_unlock_irqrestore(&table->lock, flags);
  2917. iommu_flush_irt(iommu, devid);
  2918. iommu_completion_wait(iommu);
  2919. return 0;
  2920. }
  2921. static void free_irte(u16 devid, int index)
  2922. {
  2923. struct irq_remap_table *table;
  2924. struct amd_iommu *iommu;
  2925. unsigned long flags;
  2926. iommu = amd_iommu_rlookup_table[devid];
  2927. if (iommu == NULL)
  2928. return;
  2929. table = get_irq_table(devid, false);
  2930. if (!table)
  2931. return;
  2932. spin_lock_irqsave(&table->lock, flags);
  2933. table->table[index] = 0;
  2934. spin_unlock_irqrestore(&table->lock, flags);
  2935. iommu_flush_irt(iommu, devid);
  2936. iommu_completion_wait(iommu);
  2937. }
  2938. static int get_devid(struct irq_alloc_info *info)
  2939. {
  2940. int devid = -1;
  2941. switch (info->type) {
  2942. case X86_IRQ_ALLOC_TYPE_IOAPIC:
  2943. devid = get_ioapic_devid(info->ioapic_id);
  2944. break;
  2945. case X86_IRQ_ALLOC_TYPE_HPET:
  2946. devid = get_hpet_devid(info->hpet_id);
  2947. break;
  2948. case X86_IRQ_ALLOC_TYPE_MSI:
  2949. case X86_IRQ_ALLOC_TYPE_MSIX:
  2950. devid = get_device_id(&info->msi_dev->dev);
  2951. break;
  2952. default:
  2953. BUG_ON(1);
  2954. break;
  2955. }
  2956. return devid;
  2957. }
  2958. static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info)
  2959. {
  2960. struct amd_iommu *iommu;
  2961. int devid;
  2962. if (!info)
  2963. return NULL;
  2964. devid = get_devid(info);
  2965. if (devid >= 0) {
  2966. iommu = amd_iommu_rlookup_table[devid];
  2967. if (iommu)
  2968. return iommu->ir_domain;
  2969. }
  2970. return NULL;
  2971. }
  2972. static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
  2973. {
  2974. struct amd_iommu *iommu;
  2975. int devid;
  2976. if (!info)
  2977. return NULL;
  2978. switch (info->type) {
  2979. case X86_IRQ_ALLOC_TYPE_MSI:
  2980. case X86_IRQ_ALLOC_TYPE_MSIX:
  2981. devid = get_device_id(&info->msi_dev->dev);
  2982. if (devid < 0)
  2983. return NULL;
  2984. iommu = amd_iommu_rlookup_table[devid];
  2985. if (iommu)
  2986. return iommu->msi_domain;
  2987. break;
  2988. default:
  2989. break;
  2990. }
  2991. return NULL;
  2992. }
  2993. struct irq_remap_ops amd_iommu_irq_ops = {
  2994. .prepare = amd_iommu_prepare,
  2995. .enable = amd_iommu_enable,
  2996. .disable = amd_iommu_disable,
  2997. .reenable = amd_iommu_reenable,
  2998. .enable_faulting = amd_iommu_enable_faulting,
  2999. .get_ir_irq_domain = get_ir_irq_domain,
  3000. .get_irq_domain = get_irq_domain,
  3001. };
  3002. static void irq_remapping_prepare_irte(struct amd_ir_data *data,
  3003. struct irq_cfg *irq_cfg,
  3004. struct irq_alloc_info *info,
  3005. int devid, int index, int sub_handle)
  3006. {
  3007. struct irq_2_irte *irte_info = &data->irq_2_irte;
  3008. struct msi_msg *msg = &data->msi_entry;
  3009. union irte *irte = &data->irte_entry;
  3010. struct IO_APIC_route_entry *entry;
  3011. data->irq_2_irte.devid = devid;
  3012. data->irq_2_irte.index = index + sub_handle;
  3013. /* Setup IRTE for IOMMU */
  3014. irte->val = 0;
  3015. irte->fields.vector = irq_cfg->vector;
  3016. irte->fields.int_type = apic->irq_delivery_mode;
  3017. irte->fields.destination = irq_cfg->dest_apicid;
  3018. irte->fields.dm = apic->irq_dest_mode;
  3019. irte->fields.valid = 1;
  3020. switch (info->type) {
  3021. case X86_IRQ_ALLOC_TYPE_IOAPIC:
  3022. /* Setup IOAPIC entry */
  3023. entry = info->ioapic_entry;
  3024. info->ioapic_entry = NULL;
  3025. memset(entry, 0, sizeof(*entry));
  3026. entry->vector = index;
  3027. entry->mask = 0;
  3028. entry->trigger = info->ioapic_trigger;
  3029. entry->polarity = info->ioapic_polarity;
  3030. /* Mask level triggered irqs. */
  3031. if (info->ioapic_trigger)
  3032. entry->mask = 1;
  3033. break;
  3034. case X86_IRQ_ALLOC_TYPE_HPET:
  3035. case X86_IRQ_ALLOC_TYPE_MSI:
  3036. case X86_IRQ_ALLOC_TYPE_MSIX:
  3037. msg->address_hi = MSI_ADDR_BASE_HI;
  3038. msg->address_lo = MSI_ADDR_BASE_LO;
  3039. msg->data = irte_info->index;
  3040. break;
  3041. default:
  3042. BUG_ON(1);
  3043. break;
  3044. }
  3045. }
  3046. static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
  3047. unsigned int nr_irqs, void *arg)
  3048. {
  3049. struct irq_alloc_info *info = arg;
  3050. struct irq_data *irq_data;
  3051. struct amd_ir_data *data;
  3052. struct irq_cfg *cfg;
  3053. int i, ret, devid;
  3054. int index = -1;
  3055. if (!info)
  3056. return -EINVAL;
  3057. if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
  3058. info->type != X86_IRQ_ALLOC_TYPE_MSIX)
  3059. return -EINVAL;
  3060. /*
  3061. * With IRQ remapping enabled, don't need contiguous CPU vectors
  3062. * to support multiple MSI interrupts.
  3063. */
  3064. if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
  3065. info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
  3066. devid = get_devid(info);
  3067. if (devid < 0)
  3068. return -EINVAL;
  3069. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
  3070. if (ret < 0)
  3071. return ret;
  3072. if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
  3073. if (get_irq_table(devid, true))
  3074. index = info->ioapic_pin;
  3075. else
  3076. ret = -ENOMEM;
  3077. } else {
  3078. index = alloc_irq_index(devid, nr_irqs);
  3079. }
  3080. if (index < 0) {
  3081. pr_warn("Failed to allocate IRTE\n");
  3082. goto out_free_parent;
  3083. }
  3084. for (i = 0; i < nr_irqs; i++) {
  3085. irq_data = irq_domain_get_irq_data(domain, virq + i);
  3086. cfg = irqd_cfg(irq_data);
  3087. if (!irq_data || !cfg) {
  3088. ret = -EINVAL;
  3089. goto out_free_data;
  3090. }
  3091. ret = -ENOMEM;
  3092. data = kzalloc(sizeof(*data), GFP_KERNEL);
  3093. if (!data)
  3094. goto out_free_data;
  3095. irq_data->hwirq = (devid << 16) + i;
  3096. irq_data->chip_data = data;
  3097. irq_data->chip = &amd_ir_chip;
  3098. irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
  3099. irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
  3100. }
  3101. return 0;
  3102. out_free_data:
  3103. for (i--; i >= 0; i--) {
  3104. irq_data = irq_domain_get_irq_data(domain, virq + i);
  3105. if (irq_data)
  3106. kfree(irq_data->chip_data);
  3107. }
  3108. for (i = 0; i < nr_irqs; i++)
  3109. free_irte(devid, index + i);
  3110. out_free_parent:
  3111. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  3112. return ret;
  3113. }
  3114. static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
  3115. unsigned int nr_irqs)
  3116. {
  3117. struct irq_2_irte *irte_info;
  3118. struct irq_data *irq_data;
  3119. struct amd_ir_data *data;
  3120. int i;
  3121. for (i = 0; i < nr_irqs; i++) {
  3122. irq_data = irq_domain_get_irq_data(domain, virq + i);
  3123. if (irq_data && irq_data->chip_data) {
  3124. data = irq_data->chip_data;
  3125. irte_info = &data->irq_2_irte;
  3126. free_irte(irte_info->devid, irte_info->index);
  3127. kfree(data);
  3128. }
  3129. }
  3130. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  3131. }
  3132. static void irq_remapping_activate(struct irq_domain *domain,
  3133. struct irq_data *irq_data)
  3134. {
  3135. struct amd_ir_data *data = irq_data->chip_data;
  3136. struct irq_2_irte *irte_info = &data->irq_2_irte;
  3137. modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
  3138. }
  3139. static void irq_remapping_deactivate(struct irq_domain *domain,
  3140. struct irq_data *irq_data)
  3141. {
  3142. struct amd_ir_data *data = irq_data->chip_data;
  3143. struct irq_2_irte *irte_info = &data->irq_2_irte;
  3144. union irte entry;
  3145. entry.val = 0;
  3146. modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
  3147. }
  3148. static struct irq_domain_ops amd_ir_domain_ops = {
  3149. .alloc = irq_remapping_alloc,
  3150. .free = irq_remapping_free,
  3151. .activate = irq_remapping_activate,
  3152. .deactivate = irq_remapping_deactivate,
  3153. };
  3154. static int amd_ir_set_affinity(struct irq_data *data,
  3155. const struct cpumask *mask, bool force)
  3156. {
  3157. struct amd_ir_data *ir_data = data->chip_data;
  3158. struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
  3159. struct irq_cfg *cfg = irqd_cfg(data);
  3160. struct irq_data *parent = data->parent_data;
  3161. int ret;
  3162. ret = parent->chip->irq_set_affinity(parent, mask, force);
  3163. if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
  3164. return ret;
  3165. /*
  3166. * Atomically updates the IRTE with the new destination, vector
  3167. * and flushes the interrupt entry cache.
  3168. */
  3169. ir_data->irte_entry.fields.vector = cfg->vector;
  3170. ir_data->irte_entry.fields.destination = cfg->dest_apicid;
  3171. modify_irte(irte_info->devid, irte_info->index, ir_data->irte_entry);
  3172. /*
  3173. * After this point, all the interrupts will start arriving
  3174. * at the new destination. So, time to cleanup the previous
  3175. * vector allocation.
  3176. */
  3177. send_cleanup_vector(cfg);
  3178. return IRQ_SET_MASK_OK_DONE;
  3179. }
  3180. static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
  3181. {
  3182. struct amd_ir_data *ir_data = irq_data->chip_data;
  3183. *msg = ir_data->msi_entry;
  3184. }
  3185. static struct irq_chip amd_ir_chip = {
  3186. .irq_ack = ir_ack_apic_edge,
  3187. .irq_set_affinity = amd_ir_set_affinity,
  3188. .irq_compose_msi_msg = ir_compose_msi_msg,
  3189. };
  3190. int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
  3191. {
  3192. iommu->ir_domain = irq_domain_add_tree(NULL, &amd_ir_domain_ops, iommu);
  3193. if (!iommu->ir_domain)
  3194. return -ENOMEM;
  3195. iommu->ir_domain->parent = arch_get_ir_parent_domain();
  3196. iommu->msi_domain = arch_create_msi_irq_domain(iommu->ir_domain);
  3197. return 0;
  3198. }
  3199. #endif