pci_sun4v.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  1. /* pci_sun4v.c: SUN4V specific PCI controller support.
  2. *
  3. * Copyright (C) 2006, 2007, 2008 David S. Miller (davem@davemloft.net)
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/types.h>
  7. #include <linux/pci.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/percpu.h>
  12. #include <linux/irq.h>
  13. #include <linux/msi.h>
  14. #include <linux/export.h>
  15. #include <linux/log2.h>
  16. #include <linux/of_device.h>
  17. #include <linux/iommu-common.h>
  18. #include <asm/iommu.h>
  19. #include <asm/irq.h>
  20. #include <asm/hypervisor.h>
  21. #include <asm/prom.h>
  22. #include "pci_impl.h"
  23. #include "iommu_common.h"
  24. #include "pci_sun4v.h"
  25. #define DRIVER_NAME "pci_sun4v"
  26. #define PFX DRIVER_NAME ": "
  27. static unsigned long vpci_major;
  28. static unsigned long vpci_minor;
  29. struct vpci_version {
  30. unsigned long major;
  31. unsigned long minor;
  32. };
  33. /* Ordered from largest major to lowest */
  34. static struct vpci_version vpci_versions[] = {
  35. { .major = 2, .minor = 0 },
  36. { .major = 1, .minor = 1 },
  37. };
  38. static unsigned long vatu_major = 1;
  39. static unsigned long vatu_minor = 1;
  40. #define PGLIST_NENTS (PAGE_SIZE / sizeof(u64))
  41. struct iommu_batch {
  42. struct device *dev; /* Device mapping is for. */
  43. unsigned long prot; /* IOMMU page protections */
  44. unsigned long entry; /* Index into IOTSB. */
  45. u64 *pglist; /* List of physical pages */
  46. unsigned long npages; /* Number of pages in list. */
  47. };
  48. static DEFINE_PER_CPU(struct iommu_batch, iommu_batch);
  49. static int iommu_batch_initialized;
  50. /* Interrupts must be disabled. */
  51. static inline void iommu_batch_start(struct device *dev, unsigned long prot, unsigned long entry)
  52. {
  53. struct iommu_batch *p = this_cpu_ptr(&iommu_batch);
  54. p->dev = dev;
  55. p->prot = prot;
  56. p->entry = entry;
  57. p->npages = 0;
  58. }
  59. /* Interrupts must be disabled. */
  60. static long iommu_batch_flush(struct iommu_batch *p, u64 mask)
  61. {
  62. struct pci_pbm_info *pbm = p->dev->archdata.host_controller;
  63. u64 *pglist = p->pglist;
  64. u64 index_count;
  65. unsigned long devhandle = pbm->devhandle;
  66. unsigned long prot = p->prot;
  67. unsigned long entry = p->entry;
  68. unsigned long npages = p->npages;
  69. unsigned long iotsb_num;
  70. unsigned long ret;
  71. long num;
  72. /* VPCI maj=1, min=[0,1] only supports read and write */
  73. if (vpci_major < 2)
  74. prot &= (HV_PCI_MAP_ATTR_READ | HV_PCI_MAP_ATTR_WRITE);
  75. while (npages != 0) {
  76. if (mask <= DMA_BIT_MASK(32)) {
  77. num = pci_sun4v_iommu_map(devhandle,
  78. HV_PCI_TSBID(0, entry),
  79. npages,
  80. prot,
  81. __pa(pglist));
  82. if (unlikely(num < 0)) {
  83. pr_err_ratelimited("%s: IOMMU map of [%08lx:%08llx:%lx:%lx:%lx] failed with status %ld\n",
  84. __func__,
  85. devhandle,
  86. HV_PCI_TSBID(0, entry),
  87. npages, prot, __pa(pglist),
  88. num);
  89. return -1;
  90. }
  91. } else {
  92. index_count = HV_PCI_IOTSB_INDEX_COUNT(npages, entry),
  93. iotsb_num = pbm->iommu->atu->iotsb->iotsb_num;
  94. ret = pci_sun4v_iotsb_map(devhandle,
  95. iotsb_num,
  96. index_count,
  97. prot,
  98. __pa(pglist),
  99. &num);
  100. if (unlikely(ret != HV_EOK)) {
  101. pr_err_ratelimited("%s: ATU map of [%08lx:%lx:%llx:%lx:%lx] failed with status %ld\n",
  102. __func__,
  103. devhandle, iotsb_num,
  104. index_count, prot,
  105. __pa(pglist), ret);
  106. return -1;
  107. }
  108. }
  109. entry += num;
  110. npages -= num;
  111. pglist += num;
  112. }
  113. p->entry = entry;
  114. p->npages = 0;
  115. return 0;
  116. }
  117. static inline void iommu_batch_new_entry(unsigned long entry, u64 mask)
  118. {
  119. struct iommu_batch *p = this_cpu_ptr(&iommu_batch);
  120. if (p->entry + p->npages == entry)
  121. return;
  122. if (p->entry != ~0UL)
  123. iommu_batch_flush(p, mask);
  124. p->entry = entry;
  125. }
  126. /* Interrupts must be disabled. */
  127. static inline long iommu_batch_add(u64 phys_page, u64 mask)
  128. {
  129. struct iommu_batch *p = this_cpu_ptr(&iommu_batch);
  130. BUG_ON(p->npages >= PGLIST_NENTS);
  131. p->pglist[p->npages++] = phys_page;
  132. if (p->npages == PGLIST_NENTS)
  133. return iommu_batch_flush(p, mask);
  134. return 0;
  135. }
  136. /* Interrupts must be disabled. */
  137. static inline long iommu_batch_end(u64 mask)
  138. {
  139. struct iommu_batch *p = this_cpu_ptr(&iommu_batch);
  140. BUG_ON(p->npages >= PGLIST_NENTS);
  141. return iommu_batch_flush(p, mask);
  142. }
  143. static void *dma_4v_alloc_coherent(struct device *dev, size_t size,
  144. dma_addr_t *dma_addrp, gfp_t gfp,
  145. unsigned long attrs)
  146. {
  147. u64 mask;
  148. unsigned long flags, order, first_page, npages, n;
  149. unsigned long prot = 0;
  150. struct iommu *iommu;
  151. struct atu *atu;
  152. struct iommu_map_table *tbl;
  153. struct page *page;
  154. void *ret;
  155. long entry;
  156. int nid;
  157. size = IO_PAGE_ALIGN(size);
  158. order = get_order(size);
  159. if (unlikely(order >= MAX_ORDER))
  160. return NULL;
  161. npages = size >> IO_PAGE_SHIFT;
  162. if (attrs & DMA_ATTR_WEAK_ORDERING)
  163. prot = HV_PCI_MAP_ATTR_RELAXED_ORDER;
  164. nid = dev->archdata.numa_node;
  165. page = alloc_pages_node(nid, gfp, order);
  166. if (unlikely(!page))
  167. return NULL;
  168. first_page = (unsigned long) page_address(page);
  169. memset((char *)first_page, 0, PAGE_SIZE << order);
  170. iommu = dev->archdata.iommu;
  171. atu = iommu->atu;
  172. mask = dev->coherent_dma_mask;
  173. if (mask <= DMA_BIT_MASK(32))
  174. tbl = &iommu->tbl;
  175. else
  176. tbl = &atu->tbl;
  177. entry = iommu_tbl_range_alloc(dev, tbl, npages, NULL,
  178. (unsigned long)(-1), 0);
  179. if (unlikely(entry == IOMMU_ERROR_CODE))
  180. goto range_alloc_fail;
  181. *dma_addrp = (tbl->table_map_base + (entry << IO_PAGE_SHIFT));
  182. ret = (void *) first_page;
  183. first_page = __pa(first_page);
  184. local_irq_save(flags);
  185. iommu_batch_start(dev,
  186. (HV_PCI_MAP_ATTR_READ | prot |
  187. HV_PCI_MAP_ATTR_WRITE),
  188. entry);
  189. for (n = 0; n < npages; n++) {
  190. long err = iommu_batch_add(first_page + (n * PAGE_SIZE), mask);
  191. if (unlikely(err < 0L))
  192. goto iommu_map_fail;
  193. }
  194. if (unlikely(iommu_batch_end(mask) < 0L))
  195. goto iommu_map_fail;
  196. local_irq_restore(flags);
  197. return ret;
  198. iommu_map_fail:
  199. local_irq_restore(flags);
  200. iommu_tbl_range_free(tbl, *dma_addrp, npages, IOMMU_ERROR_CODE);
  201. range_alloc_fail:
  202. free_pages(first_page, order);
  203. return NULL;
  204. }
  205. unsigned long dma_4v_iotsb_bind(unsigned long devhandle,
  206. unsigned long iotsb_num,
  207. struct pci_bus *bus_dev)
  208. {
  209. struct pci_dev *pdev;
  210. unsigned long err;
  211. unsigned int bus;
  212. unsigned int device;
  213. unsigned int fun;
  214. list_for_each_entry(pdev, &bus_dev->devices, bus_list) {
  215. if (pdev->subordinate) {
  216. /* No need to bind pci bridge */
  217. dma_4v_iotsb_bind(devhandle, iotsb_num,
  218. pdev->subordinate);
  219. } else {
  220. bus = bus_dev->number;
  221. device = PCI_SLOT(pdev->devfn);
  222. fun = PCI_FUNC(pdev->devfn);
  223. err = pci_sun4v_iotsb_bind(devhandle, iotsb_num,
  224. HV_PCI_DEVICE_BUILD(bus,
  225. device,
  226. fun));
  227. /* If bind fails for one device it is going to fail
  228. * for rest of the devices because we are sharing
  229. * IOTSB. So in case of failure simply return with
  230. * error.
  231. */
  232. if (err)
  233. return err;
  234. }
  235. }
  236. return 0;
  237. }
  238. static void dma_4v_iommu_demap(struct device *dev, unsigned long devhandle,
  239. dma_addr_t dvma, unsigned long iotsb_num,
  240. unsigned long entry, unsigned long npages)
  241. {
  242. unsigned long num, flags;
  243. unsigned long ret;
  244. local_irq_save(flags);
  245. do {
  246. if (dvma <= DMA_BIT_MASK(32)) {
  247. num = pci_sun4v_iommu_demap(devhandle,
  248. HV_PCI_TSBID(0, entry),
  249. npages);
  250. } else {
  251. ret = pci_sun4v_iotsb_demap(devhandle, iotsb_num,
  252. entry, npages, &num);
  253. if (unlikely(ret != HV_EOK)) {
  254. pr_err_ratelimited("pci_iotsb_demap() failed with error: %ld\n",
  255. ret);
  256. }
  257. }
  258. entry += num;
  259. npages -= num;
  260. } while (npages != 0);
  261. local_irq_restore(flags);
  262. }
  263. static void dma_4v_free_coherent(struct device *dev, size_t size, void *cpu,
  264. dma_addr_t dvma, unsigned long attrs)
  265. {
  266. struct pci_pbm_info *pbm;
  267. struct iommu *iommu;
  268. struct atu *atu;
  269. struct iommu_map_table *tbl;
  270. unsigned long order, npages, entry;
  271. unsigned long iotsb_num;
  272. u32 devhandle;
  273. npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
  274. iommu = dev->archdata.iommu;
  275. pbm = dev->archdata.host_controller;
  276. atu = iommu->atu;
  277. devhandle = pbm->devhandle;
  278. if (dvma <= DMA_BIT_MASK(32)) {
  279. tbl = &iommu->tbl;
  280. iotsb_num = 0; /* we don't care for legacy iommu */
  281. } else {
  282. tbl = &atu->tbl;
  283. iotsb_num = atu->iotsb->iotsb_num;
  284. }
  285. entry = ((dvma - tbl->table_map_base) >> IO_PAGE_SHIFT);
  286. dma_4v_iommu_demap(dev, devhandle, dvma, iotsb_num, entry, npages);
  287. iommu_tbl_range_free(tbl, dvma, npages, IOMMU_ERROR_CODE);
  288. order = get_order(size);
  289. if (order < 10)
  290. free_pages((unsigned long)cpu, order);
  291. }
  292. static dma_addr_t dma_4v_map_page(struct device *dev, struct page *page,
  293. unsigned long offset, size_t sz,
  294. enum dma_data_direction direction,
  295. unsigned long attrs)
  296. {
  297. struct iommu *iommu;
  298. struct atu *atu;
  299. struct iommu_map_table *tbl;
  300. u64 mask;
  301. unsigned long flags, npages, oaddr;
  302. unsigned long i, base_paddr;
  303. unsigned long prot;
  304. dma_addr_t bus_addr, ret;
  305. long entry;
  306. iommu = dev->archdata.iommu;
  307. atu = iommu->atu;
  308. if (unlikely(direction == DMA_NONE))
  309. goto bad;
  310. oaddr = (unsigned long)(page_address(page) + offset);
  311. npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
  312. npages >>= IO_PAGE_SHIFT;
  313. mask = *dev->dma_mask;
  314. if (mask <= DMA_BIT_MASK(32))
  315. tbl = &iommu->tbl;
  316. else
  317. tbl = &atu->tbl;
  318. entry = iommu_tbl_range_alloc(dev, tbl, npages, NULL,
  319. (unsigned long)(-1), 0);
  320. if (unlikely(entry == IOMMU_ERROR_CODE))
  321. goto bad;
  322. bus_addr = (tbl->table_map_base + (entry << IO_PAGE_SHIFT));
  323. ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
  324. base_paddr = __pa(oaddr & IO_PAGE_MASK);
  325. prot = HV_PCI_MAP_ATTR_READ;
  326. if (direction != DMA_TO_DEVICE)
  327. prot |= HV_PCI_MAP_ATTR_WRITE;
  328. if (attrs & DMA_ATTR_WEAK_ORDERING)
  329. prot |= HV_PCI_MAP_ATTR_RELAXED_ORDER;
  330. local_irq_save(flags);
  331. iommu_batch_start(dev, prot, entry);
  332. for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) {
  333. long err = iommu_batch_add(base_paddr, mask);
  334. if (unlikely(err < 0L))
  335. goto iommu_map_fail;
  336. }
  337. if (unlikely(iommu_batch_end(mask) < 0L))
  338. goto iommu_map_fail;
  339. local_irq_restore(flags);
  340. return ret;
  341. bad:
  342. if (printk_ratelimit())
  343. WARN_ON(1);
  344. return DMA_ERROR_CODE;
  345. iommu_map_fail:
  346. local_irq_restore(flags);
  347. iommu_tbl_range_free(tbl, bus_addr, npages, IOMMU_ERROR_CODE);
  348. return DMA_ERROR_CODE;
  349. }
  350. static void dma_4v_unmap_page(struct device *dev, dma_addr_t bus_addr,
  351. size_t sz, enum dma_data_direction direction,
  352. unsigned long attrs)
  353. {
  354. struct pci_pbm_info *pbm;
  355. struct iommu *iommu;
  356. struct atu *atu;
  357. struct iommu_map_table *tbl;
  358. unsigned long npages;
  359. unsigned long iotsb_num;
  360. long entry;
  361. u32 devhandle;
  362. if (unlikely(direction == DMA_NONE)) {
  363. if (printk_ratelimit())
  364. WARN_ON(1);
  365. return;
  366. }
  367. iommu = dev->archdata.iommu;
  368. pbm = dev->archdata.host_controller;
  369. atu = iommu->atu;
  370. devhandle = pbm->devhandle;
  371. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  372. npages >>= IO_PAGE_SHIFT;
  373. bus_addr &= IO_PAGE_MASK;
  374. if (bus_addr <= DMA_BIT_MASK(32)) {
  375. iotsb_num = 0; /* we don't care for legacy iommu */
  376. tbl = &iommu->tbl;
  377. } else {
  378. iotsb_num = atu->iotsb->iotsb_num;
  379. tbl = &atu->tbl;
  380. }
  381. entry = (bus_addr - tbl->table_map_base) >> IO_PAGE_SHIFT;
  382. dma_4v_iommu_demap(dev, devhandle, bus_addr, iotsb_num, entry, npages);
  383. iommu_tbl_range_free(tbl, bus_addr, npages, IOMMU_ERROR_CODE);
  384. }
  385. static int dma_4v_map_sg(struct device *dev, struct scatterlist *sglist,
  386. int nelems, enum dma_data_direction direction,
  387. unsigned long attrs)
  388. {
  389. struct scatterlist *s, *outs, *segstart;
  390. unsigned long flags, handle, prot;
  391. dma_addr_t dma_next = 0, dma_addr;
  392. unsigned int max_seg_size;
  393. unsigned long seg_boundary_size;
  394. int outcount, incount, i;
  395. struct iommu *iommu;
  396. struct atu *atu;
  397. struct iommu_map_table *tbl;
  398. u64 mask;
  399. unsigned long base_shift;
  400. long err;
  401. BUG_ON(direction == DMA_NONE);
  402. iommu = dev->archdata.iommu;
  403. if (nelems == 0 || !iommu)
  404. return 0;
  405. atu = iommu->atu;
  406. prot = HV_PCI_MAP_ATTR_READ;
  407. if (direction != DMA_TO_DEVICE)
  408. prot |= HV_PCI_MAP_ATTR_WRITE;
  409. if (attrs & DMA_ATTR_WEAK_ORDERING)
  410. prot |= HV_PCI_MAP_ATTR_RELAXED_ORDER;
  411. outs = s = segstart = &sglist[0];
  412. outcount = 1;
  413. incount = nelems;
  414. handle = 0;
  415. /* Init first segment length for backout at failure */
  416. outs->dma_length = 0;
  417. local_irq_save(flags);
  418. iommu_batch_start(dev, prot, ~0UL);
  419. max_seg_size = dma_get_max_seg_size(dev);
  420. seg_boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  421. IO_PAGE_SIZE) >> IO_PAGE_SHIFT;
  422. mask = *dev->dma_mask;
  423. if (mask <= DMA_BIT_MASK(32))
  424. tbl = &iommu->tbl;
  425. else
  426. tbl = &atu->tbl;
  427. base_shift = tbl->table_map_base >> IO_PAGE_SHIFT;
  428. for_each_sg(sglist, s, nelems, i) {
  429. unsigned long paddr, npages, entry, out_entry = 0, slen;
  430. slen = s->length;
  431. /* Sanity check */
  432. if (slen == 0) {
  433. dma_next = 0;
  434. continue;
  435. }
  436. /* Allocate iommu entries for that segment */
  437. paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s);
  438. npages = iommu_num_pages(paddr, slen, IO_PAGE_SIZE);
  439. entry = iommu_tbl_range_alloc(dev, tbl, npages,
  440. &handle, (unsigned long)(-1), 0);
  441. /* Handle failure */
  442. if (unlikely(entry == IOMMU_ERROR_CODE)) {
  443. pr_err_ratelimited("iommu_alloc failed, iommu %p paddr %lx npages %lx\n",
  444. tbl, paddr, npages);
  445. goto iommu_map_failed;
  446. }
  447. iommu_batch_new_entry(entry, mask);
  448. /* Convert entry to a dma_addr_t */
  449. dma_addr = tbl->table_map_base + (entry << IO_PAGE_SHIFT);
  450. dma_addr |= (s->offset & ~IO_PAGE_MASK);
  451. /* Insert into HW table */
  452. paddr &= IO_PAGE_MASK;
  453. while (npages--) {
  454. err = iommu_batch_add(paddr, mask);
  455. if (unlikely(err < 0L))
  456. goto iommu_map_failed;
  457. paddr += IO_PAGE_SIZE;
  458. }
  459. /* If we are in an open segment, try merging */
  460. if (segstart != s) {
  461. /* We cannot merge if:
  462. * - allocated dma_addr isn't contiguous to previous allocation
  463. */
  464. if ((dma_addr != dma_next) ||
  465. (outs->dma_length + s->length > max_seg_size) ||
  466. (is_span_boundary(out_entry, base_shift,
  467. seg_boundary_size, outs, s))) {
  468. /* Can't merge: create a new segment */
  469. segstart = s;
  470. outcount++;
  471. outs = sg_next(outs);
  472. } else {
  473. outs->dma_length += s->length;
  474. }
  475. }
  476. if (segstart == s) {
  477. /* This is a new segment, fill entries */
  478. outs->dma_address = dma_addr;
  479. outs->dma_length = slen;
  480. out_entry = entry;
  481. }
  482. /* Calculate next page pointer for contiguous check */
  483. dma_next = dma_addr + slen;
  484. }
  485. err = iommu_batch_end(mask);
  486. if (unlikely(err < 0L))
  487. goto iommu_map_failed;
  488. local_irq_restore(flags);
  489. if (outcount < incount) {
  490. outs = sg_next(outs);
  491. outs->dma_address = DMA_ERROR_CODE;
  492. outs->dma_length = 0;
  493. }
  494. return outcount;
  495. iommu_map_failed:
  496. for_each_sg(sglist, s, nelems, i) {
  497. if (s->dma_length != 0) {
  498. unsigned long vaddr, npages;
  499. vaddr = s->dma_address & IO_PAGE_MASK;
  500. npages = iommu_num_pages(s->dma_address, s->dma_length,
  501. IO_PAGE_SIZE);
  502. iommu_tbl_range_free(tbl, vaddr, npages,
  503. IOMMU_ERROR_CODE);
  504. /* XXX demap? XXX */
  505. s->dma_address = DMA_ERROR_CODE;
  506. s->dma_length = 0;
  507. }
  508. if (s == outs)
  509. break;
  510. }
  511. local_irq_restore(flags);
  512. return 0;
  513. }
  514. static void dma_4v_unmap_sg(struct device *dev, struct scatterlist *sglist,
  515. int nelems, enum dma_data_direction direction,
  516. unsigned long attrs)
  517. {
  518. struct pci_pbm_info *pbm;
  519. struct scatterlist *sg;
  520. struct iommu *iommu;
  521. struct atu *atu;
  522. unsigned long flags, entry;
  523. unsigned long iotsb_num;
  524. u32 devhandle;
  525. BUG_ON(direction == DMA_NONE);
  526. iommu = dev->archdata.iommu;
  527. pbm = dev->archdata.host_controller;
  528. atu = iommu->atu;
  529. devhandle = pbm->devhandle;
  530. local_irq_save(flags);
  531. sg = sglist;
  532. while (nelems--) {
  533. dma_addr_t dma_handle = sg->dma_address;
  534. unsigned int len = sg->dma_length;
  535. unsigned long npages;
  536. struct iommu_map_table *tbl;
  537. unsigned long shift = IO_PAGE_SHIFT;
  538. if (!len)
  539. break;
  540. npages = iommu_num_pages(dma_handle, len, IO_PAGE_SIZE);
  541. if (dma_handle <= DMA_BIT_MASK(32)) {
  542. iotsb_num = 0; /* we don't care for legacy iommu */
  543. tbl = &iommu->tbl;
  544. } else {
  545. iotsb_num = atu->iotsb->iotsb_num;
  546. tbl = &atu->tbl;
  547. }
  548. entry = ((dma_handle - tbl->table_map_base) >> shift);
  549. dma_4v_iommu_demap(dev, devhandle, dma_handle, iotsb_num,
  550. entry, npages);
  551. iommu_tbl_range_free(tbl, dma_handle, npages,
  552. IOMMU_ERROR_CODE);
  553. sg = sg_next(sg);
  554. }
  555. local_irq_restore(flags);
  556. }
  557. static struct dma_map_ops sun4v_dma_ops = {
  558. .alloc = dma_4v_alloc_coherent,
  559. .free = dma_4v_free_coherent,
  560. .map_page = dma_4v_map_page,
  561. .unmap_page = dma_4v_unmap_page,
  562. .map_sg = dma_4v_map_sg,
  563. .unmap_sg = dma_4v_unmap_sg,
  564. };
  565. static void pci_sun4v_scan_bus(struct pci_pbm_info *pbm, struct device *parent)
  566. {
  567. struct property *prop;
  568. struct device_node *dp;
  569. dp = pbm->op->dev.of_node;
  570. prop = of_find_property(dp, "66mhz-capable", NULL);
  571. pbm->is_66mhz_capable = (prop != NULL);
  572. pbm->pci_bus = pci_scan_one_pbm(pbm, parent);
  573. /* XXX register error interrupt handlers XXX */
  574. }
  575. static unsigned long probe_existing_entries(struct pci_pbm_info *pbm,
  576. struct iommu_map_table *iommu)
  577. {
  578. struct iommu_pool *pool;
  579. unsigned long i, pool_nr, cnt = 0;
  580. u32 devhandle;
  581. devhandle = pbm->devhandle;
  582. for (pool_nr = 0; pool_nr < iommu->nr_pools; pool_nr++) {
  583. pool = &(iommu->pools[pool_nr]);
  584. for (i = pool->start; i <= pool->end; i++) {
  585. unsigned long ret, io_attrs, ra;
  586. ret = pci_sun4v_iommu_getmap(devhandle,
  587. HV_PCI_TSBID(0, i),
  588. &io_attrs, &ra);
  589. if (ret == HV_EOK) {
  590. if (page_in_phys_avail(ra)) {
  591. pci_sun4v_iommu_demap(devhandle,
  592. HV_PCI_TSBID(0,
  593. i), 1);
  594. } else {
  595. cnt++;
  596. __set_bit(i, iommu->map);
  597. }
  598. }
  599. }
  600. }
  601. return cnt;
  602. }
  603. static int pci_sun4v_atu_alloc_iotsb(struct pci_pbm_info *pbm)
  604. {
  605. struct atu *atu = pbm->iommu->atu;
  606. struct atu_iotsb *iotsb;
  607. void *table;
  608. u64 table_size;
  609. u64 iotsb_num;
  610. unsigned long order;
  611. unsigned long err;
  612. iotsb = kzalloc(sizeof(*iotsb), GFP_KERNEL);
  613. if (!iotsb) {
  614. err = -ENOMEM;
  615. goto out_err;
  616. }
  617. atu->iotsb = iotsb;
  618. /* calculate size of IOTSB */
  619. table_size = (atu->size / IO_PAGE_SIZE) * 8;
  620. order = get_order(table_size);
  621. table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  622. if (!table) {
  623. err = -ENOMEM;
  624. goto table_failed;
  625. }
  626. iotsb->table = table;
  627. iotsb->ra = __pa(table);
  628. iotsb->dvma_size = atu->size;
  629. iotsb->dvma_base = atu->base;
  630. iotsb->table_size = table_size;
  631. iotsb->page_size = IO_PAGE_SIZE;
  632. /* configure and register IOTSB with HV */
  633. err = pci_sun4v_iotsb_conf(pbm->devhandle,
  634. iotsb->ra,
  635. iotsb->table_size,
  636. iotsb->page_size,
  637. iotsb->dvma_base,
  638. &iotsb_num);
  639. if (err) {
  640. pr_err(PFX "pci_iotsb_conf failed error: %ld\n", err);
  641. goto iotsb_conf_failed;
  642. }
  643. iotsb->iotsb_num = iotsb_num;
  644. err = dma_4v_iotsb_bind(pbm->devhandle, iotsb_num, pbm->pci_bus);
  645. if (err) {
  646. pr_err(PFX "pci_iotsb_bind failed error: %ld\n", err);
  647. goto iotsb_conf_failed;
  648. }
  649. return 0;
  650. iotsb_conf_failed:
  651. free_pages((unsigned long)table, order);
  652. table_failed:
  653. kfree(iotsb);
  654. out_err:
  655. return err;
  656. }
  657. static int pci_sun4v_atu_init(struct pci_pbm_info *pbm)
  658. {
  659. struct atu *atu = pbm->iommu->atu;
  660. unsigned long err;
  661. const u64 *ranges;
  662. u64 map_size, num_iotte;
  663. u64 dma_mask;
  664. const u32 *page_size;
  665. int len;
  666. ranges = of_get_property(pbm->op->dev.of_node, "iommu-address-ranges",
  667. &len);
  668. if (!ranges) {
  669. pr_err(PFX "No iommu-address-ranges\n");
  670. return -EINVAL;
  671. }
  672. page_size = of_get_property(pbm->op->dev.of_node, "iommu-pagesizes",
  673. NULL);
  674. if (!page_size) {
  675. pr_err(PFX "No iommu-pagesizes\n");
  676. return -EINVAL;
  677. }
  678. /* There are 4 iommu-address-ranges supported. Each range is pair of
  679. * {base, size}. The ranges[0] and ranges[1] are 32bit address space
  680. * while ranges[2] and ranges[3] are 64bit space. We want to use 64bit
  681. * address ranges to support 64bit addressing. Because 'size' for
  682. * address ranges[2] and ranges[3] are same we can select either of
  683. * ranges[2] or ranges[3] for mapping. However due to 'size' is too
  684. * large for OS to allocate IOTSB we are using fix size 32G
  685. * (ATU_64_SPACE_SIZE) which is more than enough for all PCIe devices
  686. * to share.
  687. */
  688. atu->ranges = (struct atu_ranges *)ranges;
  689. atu->base = atu->ranges[3].base;
  690. atu->size = ATU_64_SPACE_SIZE;
  691. /* Create IOTSB */
  692. err = pci_sun4v_atu_alloc_iotsb(pbm);
  693. if (err) {
  694. pr_err(PFX "Error creating ATU IOTSB\n");
  695. return err;
  696. }
  697. /* Create ATU iommu map.
  698. * One bit represents one iotte in IOTSB table.
  699. */
  700. dma_mask = (roundup_pow_of_two(atu->size) - 1UL);
  701. num_iotte = atu->size / IO_PAGE_SIZE;
  702. map_size = num_iotte / 8;
  703. atu->tbl.table_map_base = atu->base;
  704. atu->dma_addr_mask = dma_mask;
  705. atu->tbl.map = kzalloc(map_size, GFP_KERNEL);
  706. if (!atu->tbl.map)
  707. return -ENOMEM;
  708. iommu_tbl_pool_init(&atu->tbl, num_iotte, IO_PAGE_SHIFT,
  709. NULL, false /* no large_pool */,
  710. 0 /* default npools */,
  711. false /* want span boundary checking */);
  712. return 0;
  713. }
  714. static int pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
  715. {
  716. static const u32 vdma_default[] = { 0x80000000, 0x80000000 };
  717. struct iommu *iommu = pbm->iommu;
  718. unsigned long num_tsb_entries, sz;
  719. u32 dma_mask, dma_offset;
  720. const u32 *vdma;
  721. vdma = of_get_property(pbm->op->dev.of_node, "virtual-dma", NULL);
  722. if (!vdma)
  723. vdma = vdma_default;
  724. if ((vdma[0] | vdma[1]) & ~IO_PAGE_MASK) {
  725. printk(KERN_ERR PFX "Strange virtual-dma[%08x:%08x].\n",
  726. vdma[0], vdma[1]);
  727. return -EINVAL;
  728. }
  729. dma_mask = (roundup_pow_of_two(vdma[1]) - 1UL);
  730. num_tsb_entries = vdma[1] / IO_PAGE_SIZE;
  731. dma_offset = vdma[0];
  732. /* Setup initial software IOMMU state. */
  733. spin_lock_init(&iommu->lock);
  734. iommu->ctx_lowest_free = 1;
  735. iommu->tbl.table_map_base = dma_offset;
  736. iommu->dma_addr_mask = dma_mask;
  737. /* Allocate and initialize the free area map. */
  738. sz = (num_tsb_entries + 7) / 8;
  739. sz = (sz + 7UL) & ~7UL;
  740. iommu->tbl.map = kzalloc(sz, GFP_KERNEL);
  741. if (!iommu->tbl.map) {
  742. printk(KERN_ERR PFX "Error, kmalloc(arena.map) failed.\n");
  743. return -ENOMEM;
  744. }
  745. iommu_tbl_pool_init(&iommu->tbl, num_tsb_entries, IO_PAGE_SHIFT,
  746. NULL, false /* no large_pool */,
  747. 0 /* default npools */,
  748. false /* want span boundary checking */);
  749. sz = probe_existing_entries(pbm, &iommu->tbl);
  750. if (sz)
  751. printk("%s: Imported %lu TSB entries from OBP\n",
  752. pbm->name, sz);
  753. return 0;
  754. }
  755. #ifdef CONFIG_PCI_MSI
  756. struct pci_sun4v_msiq_entry {
  757. u64 version_type;
  758. #define MSIQ_VERSION_MASK 0xffffffff00000000UL
  759. #define MSIQ_VERSION_SHIFT 32
  760. #define MSIQ_TYPE_MASK 0x00000000000000ffUL
  761. #define MSIQ_TYPE_SHIFT 0
  762. #define MSIQ_TYPE_NONE 0x00
  763. #define MSIQ_TYPE_MSG 0x01
  764. #define MSIQ_TYPE_MSI32 0x02
  765. #define MSIQ_TYPE_MSI64 0x03
  766. #define MSIQ_TYPE_INTX 0x08
  767. #define MSIQ_TYPE_NONE2 0xff
  768. u64 intx_sysino;
  769. u64 reserved1;
  770. u64 stick;
  771. u64 req_id; /* bus/device/func */
  772. #define MSIQ_REQID_BUS_MASK 0xff00UL
  773. #define MSIQ_REQID_BUS_SHIFT 8
  774. #define MSIQ_REQID_DEVICE_MASK 0x00f8UL
  775. #define MSIQ_REQID_DEVICE_SHIFT 3
  776. #define MSIQ_REQID_FUNC_MASK 0x0007UL
  777. #define MSIQ_REQID_FUNC_SHIFT 0
  778. u64 msi_address;
  779. /* The format of this value is message type dependent.
  780. * For MSI bits 15:0 are the data from the MSI packet.
  781. * For MSI-X bits 31:0 are the data from the MSI packet.
  782. * For MSG, the message code and message routing code where:
  783. * bits 39:32 is the bus/device/fn of the msg target-id
  784. * bits 18:16 is the message routing code
  785. * bits 7:0 is the message code
  786. * For INTx the low order 2-bits are:
  787. * 00 - INTA
  788. * 01 - INTB
  789. * 10 - INTC
  790. * 11 - INTD
  791. */
  792. u64 msi_data;
  793. u64 reserved2;
  794. };
  795. static int pci_sun4v_get_head(struct pci_pbm_info *pbm, unsigned long msiqid,
  796. unsigned long *head)
  797. {
  798. unsigned long err, limit;
  799. err = pci_sun4v_msiq_gethead(pbm->devhandle, msiqid, head);
  800. if (unlikely(err))
  801. return -ENXIO;
  802. limit = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
  803. if (unlikely(*head >= limit))
  804. return -EFBIG;
  805. return 0;
  806. }
  807. static int pci_sun4v_dequeue_msi(struct pci_pbm_info *pbm,
  808. unsigned long msiqid, unsigned long *head,
  809. unsigned long *msi)
  810. {
  811. struct pci_sun4v_msiq_entry *ep;
  812. unsigned long err, type;
  813. /* Note: void pointer arithmetic, 'head' is a byte offset */
  814. ep = (pbm->msi_queues + ((msiqid - pbm->msiq_first) *
  815. (pbm->msiq_ent_count *
  816. sizeof(struct pci_sun4v_msiq_entry))) +
  817. *head);
  818. if ((ep->version_type & MSIQ_TYPE_MASK) == 0)
  819. return 0;
  820. type = (ep->version_type & MSIQ_TYPE_MASK) >> MSIQ_TYPE_SHIFT;
  821. if (unlikely(type != MSIQ_TYPE_MSI32 &&
  822. type != MSIQ_TYPE_MSI64))
  823. return -EINVAL;
  824. *msi = ep->msi_data;
  825. err = pci_sun4v_msi_setstate(pbm->devhandle,
  826. ep->msi_data /* msi_num */,
  827. HV_MSISTATE_IDLE);
  828. if (unlikely(err))
  829. return -ENXIO;
  830. /* Clear the entry. */
  831. ep->version_type &= ~MSIQ_TYPE_MASK;
  832. (*head) += sizeof(struct pci_sun4v_msiq_entry);
  833. if (*head >=
  834. (pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry)))
  835. *head = 0;
  836. return 1;
  837. }
  838. static int pci_sun4v_set_head(struct pci_pbm_info *pbm, unsigned long msiqid,
  839. unsigned long head)
  840. {
  841. unsigned long err;
  842. err = pci_sun4v_msiq_sethead(pbm->devhandle, msiqid, head);
  843. if (unlikely(err))
  844. return -EINVAL;
  845. return 0;
  846. }
  847. static int pci_sun4v_msi_setup(struct pci_pbm_info *pbm, unsigned long msiqid,
  848. unsigned long msi, int is_msi64)
  849. {
  850. if (pci_sun4v_msi_setmsiq(pbm->devhandle, msi, msiqid,
  851. (is_msi64 ?
  852. HV_MSITYPE_MSI64 : HV_MSITYPE_MSI32)))
  853. return -ENXIO;
  854. if (pci_sun4v_msi_setstate(pbm->devhandle, msi, HV_MSISTATE_IDLE))
  855. return -ENXIO;
  856. if (pci_sun4v_msi_setvalid(pbm->devhandle, msi, HV_MSIVALID_VALID))
  857. return -ENXIO;
  858. return 0;
  859. }
  860. static int pci_sun4v_msi_teardown(struct pci_pbm_info *pbm, unsigned long msi)
  861. {
  862. unsigned long err, msiqid;
  863. err = pci_sun4v_msi_getmsiq(pbm->devhandle, msi, &msiqid);
  864. if (err)
  865. return -ENXIO;
  866. pci_sun4v_msi_setvalid(pbm->devhandle, msi, HV_MSIVALID_INVALID);
  867. return 0;
  868. }
  869. static int pci_sun4v_msiq_alloc(struct pci_pbm_info *pbm)
  870. {
  871. unsigned long q_size, alloc_size, pages, order;
  872. int i;
  873. q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
  874. alloc_size = (pbm->msiq_num * q_size);
  875. order = get_order(alloc_size);
  876. pages = __get_free_pages(GFP_KERNEL | __GFP_COMP, order);
  877. if (pages == 0UL) {
  878. printk(KERN_ERR "MSI: Cannot allocate MSI queues (o=%lu).\n",
  879. order);
  880. return -ENOMEM;
  881. }
  882. memset((char *)pages, 0, PAGE_SIZE << order);
  883. pbm->msi_queues = (void *) pages;
  884. for (i = 0; i < pbm->msiq_num; i++) {
  885. unsigned long err, base = __pa(pages + (i * q_size));
  886. unsigned long ret1, ret2;
  887. err = pci_sun4v_msiq_conf(pbm->devhandle,
  888. pbm->msiq_first + i,
  889. base, pbm->msiq_ent_count);
  890. if (err) {
  891. printk(KERN_ERR "MSI: msiq register fails (err=%lu)\n",
  892. err);
  893. goto h_error;
  894. }
  895. err = pci_sun4v_msiq_info(pbm->devhandle,
  896. pbm->msiq_first + i,
  897. &ret1, &ret2);
  898. if (err) {
  899. printk(KERN_ERR "MSI: Cannot read msiq (err=%lu)\n",
  900. err);
  901. goto h_error;
  902. }
  903. if (ret1 != base || ret2 != pbm->msiq_ent_count) {
  904. printk(KERN_ERR "MSI: Bogus qconf "
  905. "expected[%lx:%x] got[%lx:%lx]\n",
  906. base, pbm->msiq_ent_count,
  907. ret1, ret2);
  908. goto h_error;
  909. }
  910. }
  911. return 0;
  912. h_error:
  913. free_pages(pages, order);
  914. return -EINVAL;
  915. }
  916. static void pci_sun4v_msiq_free(struct pci_pbm_info *pbm)
  917. {
  918. unsigned long q_size, alloc_size, pages, order;
  919. int i;
  920. for (i = 0; i < pbm->msiq_num; i++) {
  921. unsigned long msiqid = pbm->msiq_first + i;
  922. (void) pci_sun4v_msiq_conf(pbm->devhandle, msiqid, 0UL, 0);
  923. }
  924. q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
  925. alloc_size = (pbm->msiq_num * q_size);
  926. order = get_order(alloc_size);
  927. pages = (unsigned long) pbm->msi_queues;
  928. free_pages(pages, order);
  929. pbm->msi_queues = NULL;
  930. }
  931. static int pci_sun4v_msiq_build_irq(struct pci_pbm_info *pbm,
  932. unsigned long msiqid,
  933. unsigned long devino)
  934. {
  935. unsigned int irq = sun4v_build_irq(pbm->devhandle, devino);
  936. if (!irq)
  937. return -ENOMEM;
  938. if (pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_VALID))
  939. return -EINVAL;
  940. if (pci_sun4v_msiq_setstate(pbm->devhandle, msiqid, HV_MSIQSTATE_IDLE))
  941. return -EINVAL;
  942. return irq;
  943. }
  944. static const struct sparc64_msiq_ops pci_sun4v_msiq_ops = {
  945. .get_head = pci_sun4v_get_head,
  946. .dequeue_msi = pci_sun4v_dequeue_msi,
  947. .set_head = pci_sun4v_set_head,
  948. .msi_setup = pci_sun4v_msi_setup,
  949. .msi_teardown = pci_sun4v_msi_teardown,
  950. .msiq_alloc = pci_sun4v_msiq_alloc,
  951. .msiq_free = pci_sun4v_msiq_free,
  952. .msiq_build_irq = pci_sun4v_msiq_build_irq,
  953. };
  954. static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
  955. {
  956. sparc64_pbm_msi_init(pbm, &pci_sun4v_msiq_ops);
  957. }
  958. #else /* CONFIG_PCI_MSI */
  959. static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
  960. {
  961. }
  962. #endif /* !(CONFIG_PCI_MSI) */
  963. static int pci_sun4v_pbm_init(struct pci_pbm_info *pbm,
  964. struct platform_device *op, u32 devhandle)
  965. {
  966. struct device_node *dp = op->dev.of_node;
  967. int err;
  968. pbm->numa_node = of_node_to_nid(dp);
  969. pbm->pci_ops = &sun4v_pci_ops;
  970. pbm->config_space_reg_bits = 12;
  971. pbm->index = pci_num_pbms++;
  972. pbm->op = op;
  973. pbm->devhandle = devhandle;
  974. pbm->name = dp->full_name;
  975. printk("%s: SUN4V PCI Bus Module\n", pbm->name);
  976. printk("%s: On NUMA node %d\n", pbm->name, pbm->numa_node);
  977. pci_determine_mem_io_space(pbm);
  978. pci_get_pbm_props(pbm);
  979. err = pci_sun4v_iommu_init(pbm);
  980. if (err)
  981. return err;
  982. pci_sun4v_msi_init(pbm);
  983. pci_sun4v_scan_bus(pbm, &op->dev);
  984. /* if atu_init fails its not complete failure.
  985. * we can still continue using legacy iommu.
  986. */
  987. if (pbm->iommu->atu) {
  988. err = pci_sun4v_atu_init(pbm);
  989. if (err) {
  990. kfree(pbm->iommu->atu);
  991. pbm->iommu->atu = NULL;
  992. pr_err(PFX "ATU init failed, err=%d\n", err);
  993. }
  994. }
  995. pbm->next = pci_pbm_root;
  996. pci_pbm_root = pbm;
  997. return 0;
  998. }
  999. static int pci_sun4v_probe(struct platform_device *op)
  1000. {
  1001. const struct linux_prom64_registers *regs;
  1002. static int hvapi_negotiated = 0;
  1003. struct pci_pbm_info *pbm;
  1004. struct device_node *dp;
  1005. struct iommu *iommu;
  1006. struct atu *atu;
  1007. u32 devhandle;
  1008. int i, err = -ENODEV;
  1009. static bool hv_atu = true;
  1010. dp = op->dev.of_node;
  1011. if (!hvapi_negotiated++) {
  1012. for (i = 0; i < ARRAY_SIZE(vpci_versions); i++) {
  1013. vpci_major = vpci_versions[i].major;
  1014. vpci_minor = vpci_versions[i].minor;
  1015. err = sun4v_hvapi_register(HV_GRP_PCI, vpci_major,
  1016. &vpci_minor);
  1017. if (!err)
  1018. break;
  1019. }
  1020. if (err) {
  1021. pr_err(PFX "Could not register hvapi, err=%d\n", err);
  1022. return err;
  1023. }
  1024. pr_info(PFX "Registered hvapi major[%lu] minor[%lu]\n",
  1025. vpci_major, vpci_minor);
  1026. err = sun4v_hvapi_register(HV_GRP_ATU, vatu_major, &vatu_minor);
  1027. if (err) {
  1028. /* don't return an error if we fail to register the
  1029. * ATU group, but ATU hcalls won't be available.
  1030. */
  1031. hv_atu = false;
  1032. pr_err(PFX "Could not register hvapi ATU err=%d\n",
  1033. err);
  1034. } else {
  1035. pr_info(PFX "Registered hvapi ATU major[%lu] minor[%lu]\n",
  1036. vatu_major, vatu_minor);
  1037. }
  1038. dma_ops = &sun4v_dma_ops;
  1039. }
  1040. regs = of_get_property(dp, "reg", NULL);
  1041. err = -ENODEV;
  1042. if (!regs) {
  1043. printk(KERN_ERR PFX "Could not find config registers\n");
  1044. goto out_err;
  1045. }
  1046. devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
  1047. err = -ENOMEM;
  1048. if (!iommu_batch_initialized) {
  1049. for_each_possible_cpu(i) {
  1050. unsigned long page = get_zeroed_page(GFP_KERNEL);
  1051. if (!page)
  1052. goto out_err;
  1053. per_cpu(iommu_batch, i).pglist = (u64 *) page;
  1054. }
  1055. iommu_batch_initialized = 1;
  1056. }
  1057. pbm = kzalloc(sizeof(*pbm), GFP_KERNEL);
  1058. if (!pbm) {
  1059. printk(KERN_ERR PFX "Could not allocate pci_pbm_info\n");
  1060. goto out_err;
  1061. }
  1062. iommu = kzalloc(sizeof(struct iommu), GFP_KERNEL);
  1063. if (!iommu) {
  1064. printk(KERN_ERR PFX "Could not allocate pbm iommu\n");
  1065. goto out_free_controller;
  1066. }
  1067. pbm->iommu = iommu;
  1068. iommu->atu = NULL;
  1069. if (hv_atu) {
  1070. atu = kzalloc(sizeof(*atu), GFP_KERNEL);
  1071. if (!atu)
  1072. pr_err(PFX "Could not allocate atu\n");
  1073. else
  1074. iommu->atu = atu;
  1075. }
  1076. err = pci_sun4v_pbm_init(pbm, op, devhandle);
  1077. if (err)
  1078. goto out_free_iommu;
  1079. dev_set_drvdata(&op->dev, pbm);
  1080. return 0;
  1081. out_free_iommu:
  1082. kfree(iommu->atu);
  1083. kfree(pbm->iommu);
  1084. out_free_controller:
  1085. kfree(pbm);
  1086. out_err:
  1087. return err;
  1088. }
  1089. static const struct of_device_id pci_sun4v_match[] = {
  1090. {
  1091. .name = "pci",
  1092. .compatible = "SUNW,sun4v-pci",
  1093. },
  1094. {},
  1095. };
  1096. static struct platform_driver pci_sun4v_driver = {
  1097. .driver = {
  1098. .name = DRIVER_NAME,
  1099. .of_match_table = pci_sun4v_match,
  1100. },
  1101. .probe = pci_sun4v_probe,
  1102. };
  1103. static int __init pci_sun4v_init(void)
  1104. {
  1105. return platform_driver_register(&pci_sun4v_driver);
  1106. }
  1107. subsys_initcall(pci_sun4v_init);