pci_sun4v.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. /* pci_sun4v.c: SUN4V specific PCI controller support.
  2. *
  3. * Copyright (C) 2006 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 <asm/pbm.h>
  13. #include <asm/iommu.h>
  14. #include <asm/irq.h>
  15. #include <asm/upa.h>
  16. #include <asm/pstate.h>
  17. #include <asm/oplib.h>
  18. #include <asm/hypervisor.h>
  19. #include "pci_impl.h"
  20. #include "iommu_common.h"
  21. #include "pci_sun4v.h"
  22. #define PGLIST_NENTS (PAGE_SIZE / sizeof(u64))
  23. struct pci_iommu_batch {
  24. struct pci_dev *pdev; /* Device mapping is for. */
  25. unsigned long prot; /* IOMMU page protections */
  26. unsigned long entry; /* Index into IOTSB. */
  27. u64 *pglist; /* List of physical pages */
  28. unsigned long npages; /* Number of pages in list. */
  29. };
  30. static DEFINE_PER_CPU(struct pci_iommu_batch, pci_iommu_batch);
  31. /* Interrupts must be disabled. */
  32. static inline void pci_iommu_batch_start(struct pci_dev *pdev, unsigned long prot, unsigned long entry)
  33. {
  34. struct pci_iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
  35. p->pdev = pdev;
  36. p->prot = prot;
  37. p->entry = entry;
  38. p->npages = 0;
  39. }
  40. /* Interrupts must be disabled. */
  41. static long pci_iommu_batch_flush(struct pci_iommu_batch *p)
  42. {
  43. struct pcidev_cookie *pcp = p->pdev->sysdata;
  44. unsigned long devhandle = pcp->pbm->devhandle;
  45. unsigned long prot = p->prot;
  46. unsigned long entry = p->entry;
  47. u64 *pglist = p->pglist;
  48. unsigned long npages = p->npages;
  49. while (npages != 0) {
  50. long num;
  51. num = pci_sun4v_iommu_map(devhandle, HV_PCI_TSBID(0, entry),
  52. npages, prot, __pa(pglist));
  53. if (unlikely(num < 0)) {
  54. if (printk_ratelimit())
  55. printk("pci_iommu_batch_flush: IOMMU map of "
  56. "[%08lx:%08lx:%lx:%lx:%lx] failed with "
  57. "status %ld\n",
  58. devhandle, HV_PCI_TSBID(0, entry),
  59. npages, prot, __pa(pglist), num);
  60. return -1;
  61. }
  62. entry += num;
  63. npages -= num;
  64. pglist += num;
  65. }
  66. p->entry = entry;
  67. p->npages = 0;
  68. return 0;
  69. }
  70. /* Interrupts must be disabled. */
  71. static inline long pci_iommu_batch_add(u64 phys_page)
  72. {
  73. struct pci_iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
  74. BUG_ON(p->npages >= PGLIST_NENTS);
  75. p->pglist[p->npages++] = phys_page;
  76. if (p->npages == PGLIST_NENTS)
  77. return pci_iommu_batch_flush(p);
  78. return 0;
  79. }
  80. /* Interrupts must be disabled. */
  81. static inline long pci_iommu_batch_end(void)
  82. {
  83. struct pci_iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
  84. BUG_ON(p->npages >= PGLIST_NENTS);
  85. return pci_iommu_batch_flush(p);
  86. }
  87. static long pci_arena_alloc(struct pci_iommu_arena *arena, unsigned long npages)
  88. {
  89. unsigned long n, i, start, end, limit;
  90. int pass;
  91. limit = arena->limit;
  92. start = arena->hint;
  93. pass = 0;
  94. again:
  95. n = find_next_zero_bit(arena->map, limit, start);
  96. end = n + npages;
  97. if (unlikely(end >= limit)) {
  98. if (likely(pass < 1)) {
  99. limit = start;
  100. start = 0;
  101. pass++;
  102. goto again;
  103. } else {
  104. /* Scanned the whole thing, give up. */
  105. return -1;
  106. }
  107. }
  108. for (i = n; i < end; i++) {
  109. if (test_bit(i, arena->map)) {
  110. start = i + 1;
  111. goto again;
  112. }
  113. }
  114. for (i = n; i < end; i++)
  115. __set_bit(i, arena->map);
  116. arena->hint = end;
  117. return n;
  118. }
  119. static void pci_arena_free(struct pci_iommu_arena *arena, unsigned long base, unsigned long npages)
  120. {
  121. unsigned long i;
  122. for (i = base; i < (base + npages); i++)
  123. __clear_bit(i, arena->map);
  124. }
  125. static void *pci_4v_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp, gfp_t gfp)
  126. {
  127. struct pcidev_cookie *pcp;
  128. struct pci_iommu *iommu;
  129. unsigned long flags, order, first_page, npages, n;
  130. void *ret;
  131. long entry;
  132. size = IO_PAGE_ALIGN(size);
  133. order = get_order(size);
  134. if (unlikely(order >= MAX_ORDER))
  135. return NULL;
  136. npages = size >> IO_PAGE_SHIFT;
  137. first_page = __get_free_pages(gfp, order);
  138. if (unlikely(first_page == 0UL))
  139. return NULL;
  140. memset((char *)first_page, 0, PAGE_SIZE << order);
  141. pcp = pdev->sysdata;
  142. iommu = pcp->pbm->iommu;
  143. spin_lock_irqsave(&iommu->lock, flags);
  144. entry = pci_arena_alloc(&iommu->arena, npages);
  145. spin_unlock_irqrestore(&iommu->lock, flags);
  146. if (unlikely(entry < 0L))
  147. goto arena_alloc_fail;
  148. *dma_addrp = (iommu->page_table_map_base +
  149. (entry << IO_PAGE_SHIFT));
  150. ret = (void *) first_page;
  151. first_page = __pa(first_page);
  152. local_irq_save(flags);
  153. pci_iommu_batch_start(pdev,
  154. (HV_PCI_MAP_ATTR_READ |
  155. HV_PCI_MAP_ATTR_WRITE),
  156. entry);
  157. for (n = 0; n < npages; n++) {
  158. long err = pci_iommu_batch_add(first_page + (n * PAGE_SIZE));
  159. if (unlikely(err < 0L))
  160. goto iommu_map_fail;
  161. }
  162. if (unlikely(pci_iommu_batch_end() < 0L))
  163. goto iommu_map_fail;
  164. local_irq_restore(flags);
  165. return ret;
  166. iommu_map_fail:
  167. /* Interrupts are disabled. */
  168. spin_lock(&iommu->lock);
  169. pci_arena_free(&iommu->arena, entry, npages);
  170. spin_unlock_irqrestore(&iommu->lock, flags);
  171. arena_alloc_fail:
  172. free_pages(first_page, order);
  173. return NULL;
  174. }
  175. static void pci_4v_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_t dvma)
  176. {
  177. struct pcidev_cookie *pcp;
  178. struct pci_iommu *iommu;
  179. unsigned long flags, order, npages, entry;
  180. u32 devhandle;
  181. npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
  182. pcp = pdev->sysdata;
  183. iommu = pcp->pbm->iommu;
  184. devhandle = pcp->pbm->devhandle;
  185. entry = ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  186. spin_lock_irqsave(&iommu->lock, flags);
  187. pci_arena_free(&iommu->arena, entry, npages);
  188. do {
  189. unsigned long num;
  190. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  191. npages);
  192. entry += num;
  193. npages -= num;
  194. } while (npages != 0);
  195. spin_unlock_irqrestore(&iommu->lock, flags);
  196. order = get_order(size);
  197. if (order < 10)
  198. free_pages((unsigned long)cpu, order);
  199. }
  200. static dma_addr_t pci_4v_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direction)
  201. {
  202. struct pcidev_cookie *pcp;
  203. struct pci_iommu *iommu;
  204. unsigned long flags, npages, oaddr;
  205. unsigned long i, base_paddr;
  206. u32 bus_addr, ret;
  207. unsigned long prot;
  208. long entry;
  209. pcp = pdev->sysdata;
  210. iommu = pcp->pbm->iommu;
  211. if (unlikely(direction == PCI_DMA_NONE))
  212. goto bad;
  213. oaddr = (unsigned long)ptr;
  214. npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
  215. npages >>= IO_PAGE_SHIFT;
  216. spin_lock_irqsave(&iommu->lock, flags);
  217. entry = pci_arena_alloc(&iommu->arena, npages);
  218. spin_unlock_irqrestore(&iommu->lock, flags);
  219. if (unlikely(entry < 0L))
  220. goto bad;
  221. bus_addr = (iommu->page_table_map_base +
  222. (entry << IO_PAGE_SHIFT));
  223. ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
  224. base_paddr = __pa(oaddr & IO_PAGE_MASK);
  225. prot = HV_PCI_MAP_ATTR_READ;
  226. if (direction != PCI_DMA_TODEVICE)
  227. prot |= HV_PCI_MAP_ATTR_WRITE;
  228. local_irq_save(flags);
  229. pci_iommu_batch_start(pdev, prot, entry);
  230. for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) {
  231. long err = pci_iommu_batch_add(base_paddr);
  232. if (unlikely(err < 0L))
  233. goto iommu_map_fail;
  234. }
  235. if (unlikely(pci_iommu_batch_end() < 0L))
  236. goto iommu_map_fail;
  237. local_irq_restore(flags);
  238. return ret;
  239. bad:
  240. if (printk_ratelimit())
  241. WARN_ON(1);
  242. return PCI_DMA_ERROR_CODE;
  243. iommu_map_fail:
  244. /* Interrupts are disabled. */
  245. spin_lock(&iommu->lock);
  246. pci_arena_free(&iommu->arena, entry, npages);
  247. spin_unlock_irqrestore(&iommu->lock, flags);
  248. return PCI_DMA_ERROR_CODE;
  249. }
  250. static void pci_4v_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
  251. {
  252. struct pcidev_cookie *pcp;
  253. struct pci_iommu *iommu;
  254. unsigned long flags, npages;
  255. long entry;
  256. u32 devhandle;
  257. if (unlikely(direction == PCI_DMA_NONE)) {
  258. if (printk_ratelimit())
  259. WARN_ON(1);
  260. return;
  261. }
  262. pcp = pdev->sysdata;
  263. iommu = pcp->pbm->iommu;
  264. devhandle = pcp->pbm->devhandle;
  265. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  266. npages >>= IO_PAGE_SHIFT;
  267. bus_addr &= IO_PAGE_MASK;
  268. spin_lock_irqsave(&iommu->lock, flags);
  269. entry = (bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
  270. pci_arena_free(&iommu->arena, entry, npages);
  271. do {
  272. unsigned long num;
  273. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  274. npages);
  275. entry += num;
  276. npages -= num;
  277. } while (npages != 0);
  278. spin_unlock_irqrestore(&iommu->lock, flags);
  279. }
  280. #define SG_ENT_PHYS_ADDRESS(SG) \
  281. (__pa(page_address((SG)->page)) + (SG)->offset)
  282. static inline long fill_sg(long entry, struct pci_dev *pdev,
  283. struct scatterlist *sg,
  284. int nused, int nelems, unsigned long prot)
  285. {
  286. struct scatterlist *dma_sg = sg;
  287. struct scatterlist *sg_end = sg + nelems;
  288. unsigned long flags;
  289. int i;
  290. local_irq_save(flags);
  291. pci_iommu_batch_start(pdev, prot, entry);
  292. for (i = 0; i < nused; i++) {
  293. unsigned long pteval = ~0UL;
  294. u32 dma_npages;
  295. dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
  296. dma_sg->dma_length +
  297. ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
  298. do {
  299. unsigned long offset;
  300. signed int len;
  301. /* If we are here, we know we have at least one
  302. * more page to map. So walk forward until we
  303. * hit a page crossing, and begin creating new
  304. * mappings from that spot.
  305. */
  306. for (;;) {
  307. unsigned long tmp;
  308. tmp = SG_ENT_PHYS_ADDRESS(sg);
  309. len = sg->length;
  310. if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
  311. pteval = tmp & IO_PAGE_MASK;
  312. offset = tmp & (IO_PAGE_SIZE - 1UL);
  313. break;
  314. }
  315. if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
  316. pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
  317. offset = 0UL;
  318. len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
  319. break;
  320. }
  321. sg++;
  322. }
  323. pteval = (pteval & IOPTE_PAGE);
  324. while (len > 0) {
  325. long err;
  326. err = pci_iommu_batch_add(pteval);
  327. if (unlikely(err < 0L))
  328. goto iommu_map_failed;
  329. pteval += IO_PAGE_SIZE;
  330. len -= (IO_PAGE_SIZE - offset);
  331. offset = 0;
  332. dma_npages--;
  333. }
  334. pteval = (pteval & IOPTE_PAGE) + len;
  335. sg++;
  336. /* Skip over any tail mappings we've fully mapped,
  337. * adjusting pteval along the way. Stop when we
  338. * detect a page crossing event.
  339. */
  340. while (sg < sg_end &&
  341. (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
  342. (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
  343. ((pteval ^
  344. (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
  345. pteval += sg->length;
  346. sg++;
  347. }
  348. if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
  349. pteval = ~0UL;
  350. } while (dma_npages != 0);
  351. dma_sg++;
  352. }
  353. if (unlikely(pci_iommu_batch_end() < 0L))
  354. goto iommu_map_failed;
  355. local_irq_restore(flags);
  356. return 0;
  357. iommu_map_failed:
  358. local_irq_restore(flags);
  359. return -1L;
  360. }
  361. static int pci_4v_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  362. {
  363. struct pcidev_cookie *pcp;
  364. struct pci_iommu *iommu;
  365. unsigned long flags, npages, prot;
  366. u32 dma_base;
  367. struct scatterlist *sgtmp;
  368. long entry, err;
  369. int used;
  370. /* Fast path single entry scatterlists. */
  371. if (nelems == 1) {
  372. sglist->dma_address =
  373. pci_4v_map_single(pdev,
  374. (page_address(sglist->page) + sglist->offset),
  375. sglist->length, direction);
  376. if (unlikely(sglist->dma_address == PCI_DMA_ERROR_CODE))
  377. return 0;
  378. sglist->dma_length = sglist->length;
  379. return 1;
  380. }
  381. pcp = pdev->sysdata;
  382. iommu = pcp->pbm->iommu;
  383. if (unlikely(direction == PCI_DMA_NONE))
  384. goto bad;
  385. /* Step 1: Prepare scatter list. */
  386. npages = prepare_sg(sglist, nelems);
  387. /* Step 2: Allocate a cluster and context, if necessary. */
  388. spin_lock_irqsave(&iommu->lock, flags);
  389. entry = pci_arena_alloc(&iommu->arena, npages);
  390. spin_unlock_irqrestore(&iommu->lock, flags);
  391. if (unlikely(entry < 0L))
  392. goto bad;
  393. dma_base = iommu->page_table_map_base +
  394. (entry << IO_PAGE_SHIFT);
  395. /* Step 3: Normalize DMA addresses. */
  396. used = nelems;
  397. sgtmp = sglist;
  398. while (used && sgtmp->dma_length) {
  399. sgtmp->dma_address += dma_base;
  400. sgtmp++;
  401. used--;
  402. }
  403. used = nelems - used;
  404. /* Step 4: Create the mappings. */
  405. prot = HV_PCI_MAP_ATTR_READ;
  406. if (direction != PCI_DMA_TODEVICE)
  407. prot |= HV_PCI_MAP_ATTR_WRITE;
  408. err = fill_sg(entry, pdev, sglist, used, nelems, prot);
  409. if (unlikely(err < 0L))
  410. goto iommu_map_failed;
  411. return used;
  412. bad:
  413. if (printk_ratelimit())
  414. WARN_ON(1);
  415. return 0;
  416. iommu_map_failed:
  417. spin_lock_irqsave(&iommu->lock, flags);
  418. pci_arena_free(&iommu->arena, entry, npages);
  419. spin_unlock_irqrestore(&iommu->lock, flags);
  420. return 0;
  421. }
  422. static void pci_4v_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  423. {
  424. struct pcidev_cookie *pcp;
  425. struct pci_iommu *iommu;
  426. unsigned long flags, i, npages;
  427. long entry;
  428. u32 devhandle, bus_addr;
  429. if (unlikely(direction == PCI_DMA_NONE)) {
  430. if (printk_ratelimit())
  431. WARN_ON(1);
  432. }
  433. pcp = pdev->sysdata;
  434. iommu = pcp->pbm->iommu;
  435. devhandle = pcp->pbm->devhandle;
  436. bus_addr = sglist->dma_address & IO_PAGE_MASK;
  437. for (i = 1; i < nelems; i++)
  438. if (sglist[i].dma_length == 0)
  439. break;
  440. i--;
  441. npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) -
  442. bus_addr) >> IO_PAGE_SHIFT;
  443. entry = ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  444. spin_lock_irqsave(&iommu->lock, flags);
  445. pci_arena_free(&iommu->arena, entry, npages);
  446. do {
  447. unsigned long num;
  448. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  449. npages);
  450. entry += num;
  451. npages -= num;
  452. } while (npages != 0);
  453. spin_unlock_irqrestore(&iommu->lock, flags);
  454. }
  455. static void pci_4v_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
  456. {
  457. /* Nothing to do... */
  458. }
  459. static void pci_4v_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  460. {
  461. /* Nothing to do... */
  462. }
  463. struct pci_iommu_ops pci_sun4v_iommu_ops = {
  464. .alloc_consistent = pci_4v_alloc_consistent,
  465. .free_consistent = pci_4v_free_consistent,
  466. .map_single = pci_4v_map_single,
  467. .unmap_single = pci_4v_unmap_single,
  468. .map_sg = pci_4v_map_sg,
  469. .unmap_sg = pci_4v_unmap_sg,
  470. .dma_sync_single_for_cpu = pci_4v_dma_sync_single_for_cpu,
  471. .dma_sync_sg_for_cpu = pci_4v_dma_sync_sg_for_cpu,
  472. };
  473. /* SUN4V PCI configuration space accessors. */
  474. struct pdev_entry {
  475. struct pdev_entry *next;
  476. u32 devhandle;
  477. unsigned int bus;
  478. unsigned int device;
  479. unsigned int func;
  480. };
  481. #define PDEV_HTAB_SIZE 16
  482. #define PDEV_HTAB_MASK (PDEV_HTAB_SIZE - 1)
  483. static struct pdev_entry *pdev_htab[PDEV_HTAB_SIZE];
  484. static inline unsigned int pdev_hashfn(u32 devhandle, unsigned int bus, unsigned int device, unsigned int func)
  485. {
  486. unsigned int val;
  487. val = (devhandle ^ (devhandle >> 4));
  488. val ^= bus;
  489. val ^= device;
  490. val ^= func;
  491. return val & PDEV_HTAB_MASK;
  492. }
  493. static int pdev_htab_add(u32 devhandle, unsigned int bus, unsigned int device, unsigned int func)
  494. {
  495. struct pdev_entry *p = kmalloc(sizeof(*p), GFP_KERNEL);
  496. struct pdev_entry **slot;
  497. if (!p)
  498. return -ENOMEM;
  499. slot = &pdev_htab[pdev_hashfn(devhandle, bus, device, func)];
  500. p->next = *slot;
  501. *slot = p;
  502. p->devhandle = devhandle;
  503. p->bus = bus;
  504. p->device = device;
  505. p->func = func;
  506. return 0;
  507. }
  508. /* Recursively descend into the OBP device tree, rooted at toplevel_node,
  509. * looking for a PCI device matching bus and devfn.
  510. */
  511. static int obp_find(struct linux_prom_pci_registers *pregs, int toplevel_node, unsigned int bus, unsigned int devfn)
  512. {
  513. toplevel_node = prom_getchild(toplevel_node);
  514. while (toplevel_node != 0) {
  515. int ret = obp_find(pregs, toplevel_node, bus, devfn);
  516. if (ret != 0)
  517. return ret;
  518. ret = prom_getproperty(toplevel_node, "reg", (char *) pregs,
  519. sizeof(*pregs) * PROMREG_MAX);
  520. if (ret == 0 || ret == -1)
  521. goto next_sibling;
  522. if (((pregs[0].phys_hi >> 16) & 0xff) == bus &&
  523. ((pregs[0].phys_hi >> 8) & 0xff) == devfn)
  524. break;
  525. next_sibling:
  526. toplevel_node = prom_getsibling(toplevel_node);
  527. }
  528. return toplevel_node;
  529. }
  530. static int pdev_htab_populate(struct pci_pbm_info *pbm)
  531. {
  532. struct linux_prom_pci_registers pr[PROMREG_MAX];
  533. u32 devhandle = pbm->devhandle;
  534. unsigned int bus;
  535. for (bus = pbm->pci_first_busno; bus <= pbm->pci_last_busno; bus++) {
  536. unsigned int devfn;
  537. for (devfn = 0; devfn < 256; devfn++) {
  538. unsigned int device = PCI_SLOT(devfn);
  539. unsigned int func = PCI_FUNC(devfn);
  540. if (obp_find(pr, pbm->prom_node, bus, devfn)) {
  541. int err = pdev_htab_add(devhandle, bus,
  542. device, func);
  543. if (err)
  544. return err;
  545. }
  546. }
  547. }
  548. return 0;
  549. }
  550. static struct pdev_entry *pdev_find(u32 devhandle, unsigned int bus, unsigned int device, unsigned int func)
  551. {
  552. struct pdev_entry *p;
  553. p = pdev_htab[pdev_hashfn(devhandle, bus, device, func)];
  554. while (p) {
  555. if (p->devhandle == devhandle &&
  556. p->bus == bus &&
  557. p->device == device &&
  558. p->func == func)
  559. break;
  560. p = p->next;
  561. }
  562. return p;
  563. }
  564. static inline int pci_sun4v_out_of_range(struct pci_pbm_info *pbm, unsigned int bus, unsigned int device, unsigned int func)
  565. {
  566. if (bus < pbm->pci_first_busno ||
  567. bus > pbm->pci_last_busno)
  568. return 1;
  569. return pdev_find(pbm->devhandle, bus, device, func) == NULL;
  570. }
  571. static int pci_sun4v_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  572. int where, int size, u32 *value)
  573. {
  574. struct pci_pbm_info *pbm = bus_dev->sysdata;
  575. u32 devhandle = pbm->devhandle;
  576. unsigned int bus = bus_dev->number;
  577. unsigned int device = PCI_SLOT(devfn);
  578. unsigned int func = PCI_FUNC(devfn);
  579. unsigned long ret;
  580. if (pci_sun4v_out_of_range(pbm, bus, device, func)) {
  581. ret = ~0UL;
  582. } else {
  583. ret = pci_sun4v_config_get(devhandle,
  584. HV_PCI_DEVICE_BUILD(bus, device, func),
  585. where, size);
  586. #if 0
  587. printk("rcfg: [%x:%x:%x:%d]=[%lx]\n",
  588. devhandle, HV_PCI_DEVICE_BUILD(bus, device, func),
  589. where, size, ret);
  590. #endif
  591. }
  592. switch (size) {
  593. case 1:
  594. *value = ret & 0xff;
  595. break;
  596. case 2:
  597. *value = ret & 0xffff;
  598. break;
  599. case 4:
  600. *value = ret & 0xffffffff;
  601. break;
  602. };
  603. return PCIBIOS_SUCCESSFUL;
  604. }
  605. static int pci_sun4v_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  606. int where, int size, u32 value)
  607. {
  608. struct pci_pbm_info *pbm = bus_dev->sysdata;
  609. u32 devhandle = pbm->devhandle;
  610. unsigned int bus = bus_dev->number;
  611. unsigned int device = PCI_SLOT(devfn);
  612. unsigned int func = PCI_FUNC(devfn);
  613. unsigned long ret;
  614. if (pci_sun4v_out_of_range(pbm, bus, device, func)) {
  615. /* Do nothing. */
  616. } else {
  617. ret = pci_sun4v_config_put(devhandle,
  618. HV_PCI_DEVICE_BUILD(bus, device, func),
  619. where, size, value);
  620. #if 0
  621. printk("wcfg: [%x:%x:%x:%d] v[%x] == [%lx]\n",
  622. devhandle, HV_PCI_DEVICE_BUILD(bus, device, func),
  623. where, size, value, ret);
  624. #endif
  625. }
  626. return PCIBIOS_SUCCESSFUL;
  627. }
  628. static struct pci_ops pci_sun4v_ops = {
  629. .read = pci_sun4v_read_pci_cfg,
  630. .write = pci_sun4v_write_pci_cfg,
  631. };
  632. static void pbm_scan_bus(struct pci_controller_info *p,
  633. struct pci_pbm_info *pbm)
  634. {
  635. struct pcidev_cookie *cookie = kmalloc(sizeof(*cookie), GFP_KERNEL);
  636. if (!cookie) {
  637. prom_printf("%s: Critical allocation failure.\n", pbm->name);
  638. prom_halt();
  639. }
  640. /* All we care about is the PBM. */
  641. memset(cookie, 0, sizeof(*cookie));
  642. cookie->pbm = pbm;
  643. pbm->pci_bus = pci_scan_bus(pbm->pci_first_busno, p->pci_ops, pbm);
  644. #if 0
  645. pci_fixup_host_bridge_self(pbm->pci_bus);
  646. pbm->pci_bus->self->sysdata = cookie;
  647. #endif
  648. pci_fill_in_pbm_cookies(pbm->pci_bus, pbm,
  649. pbm->prom_node);
  650. pci_record_assignments(pbm, pbm->pci_bus);
  651. pci_assign_unassigned(pbm, pbm->pci_bus);
  652. pci_fixup_irq(pbm, pbm->pci_bus);
  653. pci_determine_66mhz_disposition(pbm, pbm->pci_bus);
  654. pci_setup_busmastering(pbm, pbm->pci_bus);
  655. }
  656. static void pci_sun4v_scan_bus(struct pci_controller_info *p)
  657. {
  658. if (p->pbm_A.prom_node) {
  659. p->pbm_A.is_66mhz_capable =
  660. prom_getbool(p->pbm_A.prom_node, "66mhz-capable");
  661. pbm_scan_bus(p, &p->pbm_A);
  662. }
  663. if (p->pbm_B.prom_node) {
  664. p->pbm_B.is_66mhz_capable =
  665. prom_getbool(p->pbm_B.prom_node, "66mhz-capable");
  666. pbm_scan_bus(p, &p->pbm_B);
  667. }
  668. /* XXX register error interrupt handlers XXX */
  669. }
  670. static unsigned int pci_sun4v_irq_build(struct pci_pbm_info *pbm,
  671. struct pci_dev *pdev,
  672. unsigned int devino)
  673. {
  674. u32 devhandle = pbm->devhandle;
  675. return sun4v_build_irq(devhandle, devino, IBF_PCI);
  676. }
  677. static void pci_sun4v_base_address_update(struct pci_dev *pdev, int resource)
  678. {
  679. struct pcidev_cookie *pcp = pdev->sysdata;
  680. struct pci_pbm_info *pbm = pcp->pbm;
  681. struct resource *res, *root;
  682. u32 reg;
  683. int where, size, is_64bit;
  684. res = &pdev->resource[resource];
  685. if (resource < 6) {
  686. where = PCI_BASE_ADDRESS_0 + (resource * 4);
  687. } else if (resource == PCI_ROM_RESOURCE) {
  688. where = pdev->rom_base_reg;
  689. } else {
  690. /* Somebody might have asked allocation of a non-standard resource */
  691. return;
  692. }
  693. /* XXX 64-bit MEM handling is not %100 correct... XXX */
  694. is_64bit = 0;
  695. if (res->flags & IORESOURCE_IO)
  696. root = &pbm->io_space;
  697. else {
  698. root = &pbm->mem_space;
  699. if ((res->flags & PCI_BASE_ADDRESS_MEM_TYPE_MASK)
  700. == PCI_BASE_ADDRESS_MEM_TYPE_64)
  701. is_64bit = 1;
  702. }
  703. size = res->end - res->start;
  704. pci_read_config_dword(pdev, where, &reg);
  705. reg = ((reg & size) |
  706. (((u32)(res->start - root->start)) & ~size));
  707. if (resource == PCI_ROM_RESOURCE) {
  708. reg |= PCI_ROM_ADDRESS_ENABLE;
  709. res->flags |= IORESOURCE_ROM_ENABLE;
  710. }
  711. pci_write_config_dword(pdev, where, reg);
  712. /* This knows that the upper 32-bits of the address
  713. * must be zero. Our PCI common layer enforces this.
  714. */
  715. if (is_64bit)
  716. pci_write_config_dword(pdev, where + 4, 0);
  717. }
  718. static void pci_sun4v_resource_adjust(struct pci_dev *pdev,
  719. struct resource *res,
  720. struct resource *root)
  721. {
  722. res->start += root->start;
  723. res->end += root->start;
  724. }
  725. /* Use ranges property to determine where PCI MEM, I/O, and Config
  726. * space are for this PCI bus module.
  727. */
  728. static void pci_sun4v_determine_mem_io_space(struct pci_pbm_info *pbm)
  729. {
  730. int i, saw_mem, saw_io;
  731. saw_mem = saw_io = 0;
  732. for (i = 0; i < pbm->num_pbm_ranges; i++) {
  733. struct linux_prom_pci_ranges *pr = &pbm->pbm_ranges[i];
  734. unsigned long a;
  735. int type;
  736. type = (pr->child_phys_hi >> 24) & 0x3;
  737. a = (((unsigned long)pr->parent_phys_hi << 32UL) |
  738. ((unsigned long)pr->parent_phys_lo << 0UL));
  739. switch (type) {
  740. case 1:
  741. /* 16-bit IO space, 16MB */
  742. pbm->io_space.start = a;
  743. pbm->io_space.end = a + ((16UL*1024UL*1024UL) - 1UL);
  744. pbm->io_space.flags = IORESOURCE_IO;
  745. saw_io = 1;
  746. break;
  747. case 2:
  748. /* 32-bit MEM space, 2GB */
  749. pbm->mem_space.start = a;
  750. pbm->mem_space.end = a + (0x80000000UL - 1UL);
  751. pbm->mem_space.flags = IORESOURCE_MEM;
  752. saw_mem = 1;
  753. break;
  754. case 3:
  755. /* XXX 64-bit MEM handling XXX */
  756. default:
  757. break;
  758. };
  759. }
  760. if (!saw_io || !saw_mem) {
  761. prom_printf("%s: Fatal error, missing %s PBM range.\n",
  762. pbm->name,
  763. (!saw_io ? "IO" : "MEM"));
  764. prom_halt();
  765. }
  766. printk("%s: PCI IO[%lx] MEM[%lx]\n",
  767. pbm->name,
  768. pbm->io_space.start,
  769. pbm->mem_space.start);
  770. }
  771. static void pbm_register_toplevel_resources(struct pci_controller_info *p,
  772. struct pci_pbm_info *pbm)
  773. {
  774. pbm->io_space.name = pbm->mem_space.name = pbm->name;
  775. request_resource(&ioport_resource, &pbm->io_space);
  776. request_resource(&iomem_resource, &pbm->mem_space);
  777. pci_register_legacy_regions(&pbm->io_space,
  778. &pbm->mem_space);
  779. }
  780. static unsigned long probe_existing_entries(struct pci_pbm_info *pbm,
  781. struct pci_iommu *iommu)
  782. {
  783. struct pci_iommu_arena *arena = &iommu->arena;
  784. unsigned long i, cnt = 0;
  785. u32 devhandle;
  786. devhandle = pbm->devhandle;
  787. for (i = 0; i < arena->limit; i++) {
  788. unsigned long ret, io_attrs, ra;
  789. ret = pci_sun4v_iommu_getmap(devhandle,
  790. HV_PCI_TSBID(0, i),
  791. &io_attrs, &ra);
  792. if (ret == HV_EOK) {
  793. cnt++;
  794. __set_bit(i, arena->map);
  795. }
  796. }
  797. return cnt;
  798. }
  799. static void pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
  800. {
  801. struct pci_iommu *iommu = pbm->iommu;
  802. unsigned long num_tsb_entries, sz;
  803. u32 vdma[2], dma_mask, dma_offset;
  804. int err, tsbsize;
  805. err = prom_getproperty(pbm->prom_node, "virtual-dma",
  806. (char *)&vdma[0], sizeof(vdma));
  807. if (err == 0 || err == -1) {
  808. /* No property, use default values. */
  809. vdma[0] = 0x80000000;
  810. vdma[1] = 0x80000000;
  811. }
  812. dma_mask = vdma[0];
  813. switch (vdma[1]) {
  814. case 0x20000000:
  815. dma_mask |= 0x1fffffff;
  816. tsbsize = 64;
  817. break;
  818. case 0x40000000:
  819. dma_mask |= 0x3fffffff;
  820. tsbsize = 128;
  821. break;
  822. case 0x80000000:
  823. dma_mask |= 0x7fffffff;
  824. tsbsize = 256;
  825. break;
  826. default:
  827. prom_printf("PCI-SUN4V: strange virtual-dma size.\n");
  828. prom_halt();
  829. };
  830. tsbsize *= (8 * 1024);
  831. num_tsb_entries = tsbsize / sizeof(iopte_t);
  832. dma_offset = vdma[0];
  833. /* Setup initial software IOMMU state. */
  834. spin_lock_init(&iommu->lock);
  835. iommu->ctx_lowest_free = 1;
  836. iommu->page_table_map_base = dma_offset;
  837. iommu->dma_addr_mask = dma_mask;
  838. /* Allocate and initialize the free area map. */
  839. sz = num_tsb_entries / 8;
  840. sz = (sz + 7UL) & ~7UL;
  841. iommu->arena.map = kmalloc(sz, GFP_KERNEL);
  842. if (!iommu->arena.map) {
  843. prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
  844. prom_halt();
  845. }
  846. memset(iommu->arena.map, 0, sz);
  847. iommu->arena.limit = num_tsb_entries;
  848. sz = probe_existing_entries(pbm, iommu);
  849. printk("%s: TSB entries [%lu], existing mapings [%lu]\n",
  850. pbm->name, num_tsb_entries, sz);
  851. }
  852. static void pci_sun4v_get_bus_range(struct pci_pbm_info *pbm)
  853. {
  854. unsigned int busrange[2];
  855. int prom_node = pbm->prom_node;
  856. int err;
  857. err = prom_getproperty(prom_node, "bus-range",
  858. (char *)&busrange[0],
  859. sizeof(busrange));
  860. if (err == 0 || err == -1) {
  861. prom_printf("%s: Fatal error, no bus-range.\n", pbm->name);
  862. prom_halt();
  863. }
  864. pbm->pci_first_busno = busrange[0];
  865. pbm->pci_last_busno = busrange[1];
  866. }
  867. static void pci_sun4v_pbm_init(struct pci_controller_info *p, int prom_node, u32 devhandle)
  868. {
  869. struct pci_pbm_info *pbm;
  870. int err, i;
  871. if (devhandle & 0x40)
  872. pbm = &p->pbm_B;
  873. else
  874. pbm = &p->pbm_A;
  875. pbm->parent = p;
  876. pbm->prom_node = prom_node;
  877. pbm->pci_first_slot = 1;
  878. pbm->devhandle = devhandle;
  879. sprintf(pbm->name, "SUN4V-PCI%d PBM%c",
  880. p->index, (pbm == &p->pbm_A ? 'A' : 'B'));
  881. printk("%s: devhandle[%x] prom_node[%x:%x]\n",
  882. pbm->name, pbm->devhandle,
  883. pbm->prom_node, prom_getchild(pbm->prom_node));
  884. prom_getstring(prom_node, "name",
  885. pbm->prom_name, sizeof(pbm->prom_name));
  886. err = prom_getproperty(prom_node, "ranges",
  887. (char *) pbm->pbm_ranges,
  888. sizeof(pbm->pbm_ranges));
  889. if (err == 0 || err == -1) {
  890. prom_printf("%s: Fatal error, no ranges property.\n",
  891. pbm->name);
  892. prom_halt();
  893. }
  894. pbm->num_pbm_ranges =
  895. (err / sizeof(struct linux_prom_pci_ranges));
  896. /* Mask out the top 8 bits of the ranges, leaving the real
  897. * physical address.
  898. */
  899. for (i = 0; i < pbm->num_pbm_ranges; i++)
  900. pbm->pbm_ranges[i].parent_phys_hi &= 0x0fffffff;
  901. pci_sun4v_determine_mem_io_space(pbm);
  902. pbm_register_toplevel_resources(p, pbm);
  903. err = prom_getproperty(prom_node, "interrupt-map",
  904. (char *)pbm->pbm_intmap,
  905. sizeof(pbm->pbm_intmap));
  906. if (err == 0 || err == -1) {
  907. prom_printf("%s: Fatal error, no interrupt-map property.\n",
  908. pbm->name);
  909. prom_halt();
  910. }
  911. pbm->num_pbm_intmap = (err / sizeof(struct linux_prom_pci_intmap));
  912. err = prom_getproperty(prom_node, "interrupt-map-mask",
  913. (char *)&pbm->pbm_intmask,
  914. sizeof(pbm->pbm_intmask));
  915. if (err == 0 || err == -1) {
  916. prom_printf("%s: Fatal error, no interrupt-map-mask.\n",
  917. pbm->name);
  918. prom_halt();
  919. }
  920. pci_sun4v_get_bus_range(pbm);
  921. pci_sun4v_iommu_init(pbm);
  922. pdev_htab_populate(pbm);
  923. }
  924. void sun4v_pci_init(int node, char *model_name)
  925. {
  926. struct pci_controller_info *p;
  927. struct pci_iommu *iommu;
  928. struct linux_prom64_registers regs;
  929. u32 devhandle;
  930. int i;
  931. prom_getproperty(node, "reg", (char *)&regs, sizeof(regs));
  932. devhandle = (regs.phys_addr >> 32UL) & 0x0fffffff;
  933. for (p = pci_controller_root; p; p = p->next) {
  934. struct pci_pbm_info *pbm;
  935. if (p->pbm_A.prom_node && p->pbm_B.prom_node)
  936. continue;
  937. pbm = (p->pbm_A.prom_node ?
  938. &p->pbm_A :
  939. &p->pbm_B);
  940. if (pbm->devhandle == (devhandle ^ 0x40)) {
  941. pci_sun4v_pbm_init(p, node, devhandle);
  942. return;
  943. }
  944. }
  945. for_each_possible_cpu(i) {
  946. unsigned long page = get_zeroed_page(GFP_ATOMIC);
  947. if (!page)
  948. goto fatal_memory_error;
  949. per_cpu(pci_iommu_batch, i).pglist = (u64 *) page;
  950. }
  951. p = kmalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
  952. if (!p)
  953. goto fatal_memory_error;
  954. memset(p, 0, sizeof(*p));
  955. iommu = kmalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
  956. if (!iommu)
  957. goto fatal_memory_error;
  958. memset(iommu, 0, sizeof(*iommu));
  959. p->pbm_A.iommu = iommu;
  960. iommu = kmalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
  961. if (!iommu)
  962. goto fatal_memory_error;
  963. memset(iommu, 0, sizeof(*iommu));
  964. p->pbm_B.iommu = iommu;
  965. p->next = pci_controller_root;
  966. pci_controller_root = p;
  967. p->index = pci_num_controllers++;
  968. p->pbms_same_domain = 0;
  969. p->scan_bus = pci_sun4v_scan_bus;
  970. p->irq_build = pci_sun4v_irq_build;
  971. p->base_address_update = pci_sun4v_base_address_update;
  972. p->resource_adjust = pci_sun4v_resource_adjust;
  973. p->pci_ops = &pci_sun4v_ops;
  974. /* Like PSYCHO and SCHIZO we have a 2GB aligned area
  975. * for memory space.
  976. */
  977. pci_memspace_mask = 0x7fffffffUL;
  978. pci_sun4v_pbm_init(p, node, devhandle);
  979. return;
  980. fatal_memory_error:
  981. prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");
  982. prom_halt();
  983. }