pci_sun4v.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  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. int pil;
  676. pil = 5;
  677. if (pdev) {
  678. switch ((pdev->class >> 16) & 0xff) {
  679. case PCI_BASE_CLASS_STORAGE:
  680. pil = 5;
  681. break;
  682. case PCI_BASE_CLASS_NETWORK:
  683. pil = 6;
  684. break;
  685. case PCI_BASE_CLASS_DISPLAY:
  686. pil = 9;
  687. break;
  688. case PCI_BASE_CLASS_MULTIMEDIA:
  689. case PCI_BASE_CLASS_MEMORY:
  690. case PCI_BASE_CLASS_BRIDGE:
  691. case PCI_BASE_CLASS_SERIAL:
  692. pil = 10;
  693. break;
  694. default:
  695. pil = 5;
  696. break;
  697. };
  698. }
  699. BUG_ON(PIL_RESERVED(pil));
  700. return sun4v_build_irq(devhandle, devino, pil, IBF_PCI);
  701. }
  702. static void pci_sun4v_base_address_update(struct pci_dev *pdev, int resource)
  703. {
  704. struct pcidev_cookie *pcp = pdev->sysdata;
  705. struct pci_pbm_info *pbm = pcp->pbm;
  706. struct resource *res, *root;
  707. u32 reg;
  708. int where, size, is_64bit;
  709. res = &pdev->resource[resource];
  710. if (resource < 6) {
  711. where = PCI_BASE_ADDRESS_0 + (resource * 4);
  712. } else if (resource == PCI_ROM_RESOURCE) {
  713. where = pdev->rom_base_reg;
  714. } else {
  715. /* Somebody might have asked allocation of a non-standard resource */
  716. return;
  717. }
  718. /* XXX 64-bit MEM handling is not %100 correct... XXX */
  719. is_64bit = 0;
  720. if (res->flags & IORESOURCE_IO)
  721. root = &pbm->io_space;
  722. else {
  723. root = &pbm->mem_space;
  724. if ((res->flags & PCI_BASE_ADDRESS_MEM_TYPE_MASK)
  725. == PCI_BASE_ADDRESS_MEM_TYPE_64)
  726. is_64bit = 1;
  727. }
  728. size = res->end - res->start;
  729. pci_read_config_dword(pdev, where, &reg);
  730. reg = ((reg & size) |
  731. (((u32)(res->start - root->start)) & ~size));
  732. if (resource == PCI_ROM_RESOURCE) {
  733. reg |= PCI_ROM_ADDRESS_ENABLE;
  734. res->flags |= IORESOURCE_ROM_ENABLE;
  735. }
  736. pci_write_config_dword(pdev, where, reg);
  737. /* This knows that the upper 32-bits of the address
  738. * must be zero. Our PCI common layer enforces this.
  739. */
  740. if (is_64bit)
  741. pci_write_config_dword(pdev, where + 4, 0);
  742. }
  743. static void pci_sun4v_resource_adjust(struct pci_dev *pdev,
  744. struct resource *res,
  745. struct resource *root)
  746. {
  747. res->start += root->start;
  748. res->end += root->start;
  749. }
  750. /* Use ranges property to determine where PCI MEM, I/O, and Config
  751. * space are for this PCI bus module.
  752. */
  753. static void pci_sun4v_determine_mem_io_space(struct pci_pbm_info *pbm)
  754. {
  755. int i, saw_mem, saw_io;
  756. saw_mem = saw_io = 0;
  757. for (i = 0; i < pbm->num_pbm_ranges; i++) {
  758. struct linux_prom_pci_ranges *pr = &pbm->pbm_ranges[i];
  759. unsigned long a;
  760. int type;
  761. type = (pr->child_phys_hi >> 24) & 0x3;
  762. a = (((unsigned long)pr->parent_phys_hi << 32UL) |
  763. ((unsigned long)pr->parent_phys_lo << 0UL));
  764. switch (type) {
  765. case 1:
  766. /* 16-bit IO space, 16MB */
  767. pbm->io_space.start = a;
  768. pbm->io_space.end = a + ((16UL*1024UL*1024UL) - 1UL);
  769. pbm->io_space.flags = IORESOURCE_IO;
  770. saw_io = 1;
  771. break;
  772. case 2:
  773. /* 32-bit MEM space, 2GB */
  774. pbm->mem_space.start = a;
  775. pbm->mem_space.end = a + (0x80000000UL - 1UL);
  776. pbm->mem_space.flags = IORESOURCE_MEM;
  777. saw_mem = 1;
  778. break;
  779. case 3:
  780. /* XXX 64-bit MEM handling XXX */
  781. default:
  782. break;
  783. };
  784. }
  785. if (!saw_io || !saw_mem) {
  786. prom_printf("%s: Fatal error, missing %s PBM range.\n",
  787. pbm->name,
  788. (!saw_io ? "IO" : "MEM"));
  789. prom_halt();
  790. }
  791. printk("%s: PCI IO[%lx] MEM[%lx]\n",
  792. pbm->name,
  793. pbm->io_space.start,
  794. pbm->mem_space.start);
  795. }
  796. static void pbm_register_toplevel_resources(struct pci_controller_info *p,
  797. struct pci_pbm_info *pbm)
  798. {
  799. pbm->io_space.name = pbm->mem_space.name = pbm->name;
  800. request_resource(&ioport_resource, &pbm->io_space);
  801. request_resource(&iomem_resource, &pbm->mem_space);
  802. pci_register_legacy_regions(&pbm->io_space,
  803. &pbm->mem_space);
  804. }
  805. static unsigned long probe_existing_entries(struct pci_pbm_info *pbm,
  806. struct pci_iommu *iommu)
  807. {
  808. struct pci_iommu_arena *arena = &iommu->arena;
  809. unsigned long i, cnt = 0;
  810. u32 devhandle;
  811. devhandle = pbm->devhandle;
  812. for (i = 0; i < arena->limit; i++) {
  813. unsigned long ret, io_attrs, ra;
  814. ret = pci_sun4v_iommu_getmap(devhandle,
  815. HV_PCI_TSBID(0, i),
  816. &io_attrs, &ra);
  817. if (ret == HV_EOK) {
  818. cnt++;
  819. __set_bit(i, arena->map);
  820. }
  821. }
  822. return cnt;
  823. }
  824. static void pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
  825. {
  826. struct pci_iommu *iommu = pbm->iommu;
  827. unsigned long num_tsb_entries, sz;
  828. u32 vdma[2], dma_mask, dma_offset;
  829. int err, tsbsize;
  830. err = prom_getproperty(pbm->prom_node, "virtual-dma",
  831. (char *)&vdma[0], sizeof(vdma));
  832. if (err == 0 || err == -1) {
  833. /* No property, use default values. */
  834. vdma[0] = 0x80000000;
  835. vdma[1] = 0x80000000;
  836. }
  837. dma_mask = vdma[0];
  838. switch (vdma[1]) {
  839. case 0x20000000:
  840. dma_mask |= 0x1fffffff;
  841. tsbsize = 64;
  842. break;
  843. case 0x40000000:
  844. dma_mask |= 0x3fffffff;
  845. tsbsize = 128;
  846. break;
  847. case 0x80000000:
  848. dma_mask |= 0x7fffffff;
  849. tsbsize = 256;
  850. break;
  851. default:
  852. prom_printf("PCI-SUN4V: strange virtual-dma size.\n");
  853. prom_halt();
  854. };
  855. tsbsize *= (8 * 1024);
  856. num_tsb_entries = tsbsize / sizeof(iopte_t);
  857. dma_offset = vdma[0];
  858. /* Setup initial software IOMMU state. */
  859. spin_lock_init(&iommu->lock);
  860. iommu->ctx_lowest_free = 1;
  861. iommu->page_table_map_base = dma_offset;
  862. iommu->dma_addr_mask = dma_mask;
  863. /* Allocate and initialize the free area map. */
  864. sz = num_tsb_entries / 8;
  865. sz = (sz + 7UL) & ~7UL;
  866. iommu->arena.map = kmalloc(sz, GFP_KERNEL);
  867. if (!iommu->arena.map) {
  868. prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
  869. prom_halt();
  870. }
  871. memset(iommu->arena.map, 0, sz);
  872. iommu->arena.limit = num_tsb_entries;
  873. sz = probe_existing_entries(pbm, iommu);
  874. printk("%s: TSB entries [%lu], existing mapings [%lu]\n",
  875. pbm->name, num_tsb_entries, sz);
  876. }
  877. static void pci_sun4v_get_bus_range(struct pci_pbm_info *pbm)
  878. {
  879. unsigned int busrange[2];
  880. int prom_node = pbm->prom_node;
  881. int err;
  882. err = prom_getproperty(prom_node, "bus-range",
  883. (char *)&busrange[0],
  884. sizeof(busrange));
  885. if (err == 0 || err == -1) {
  886. prom_printf("%s: Fatal error, no bus-range.\n", pbm->name);
  887. prom_halt();
  888. }
  889. pbm->pci_first_busno = busrange[0];
  890. pbm->pci_last_busno = busrange[1];
  891. }
  892. static void pci_sun4v_pbm_init(struct pci_controller_info *p, int prom_node, u32 devhandle)
  893. {
  894. struct pci_pbm_info *pbm;
  895. int err, i;
  896. if (devhandle & 0x40)
  897. pbm = &p->pbm_B;
  898. else
  899. pbm = &p->pbm_A;
  900. pbm->parent = p;
  901. pbm->prom_node = prom_node;
  902. pbm->pci_first_slot = 1;
  903. pbm->devhandle = devhandle;
  904. sprintf(pbm->name, "SUN4V-PCI%d PBM%c",
  905. p->index, (pbm == &p->pbm_A ? 'A' : 'B'));
  906. printk("%s: devhandle[%x] prom_node[%x:%x]\n",
  907. pbm->name, pbm->devhandle,
  908. pbm->prom_node, prom_getchild(pbm->prom_node));
  909. prom_getstring(prom_node, "name",
  910. pbm->prom_name, sizeof(pbm->prom_name));
  911. err = prom_getproperty(prom_node, "ranges",
  912. (char *) pbm->pbm_ranges,
  913. sizeof(pbm->pbm_ranges));
  914. if (err == 0 || err == -1) {
  915. prom_printf("%s: Fatal error, no ranges property.\n",
  916. pbm->name);
  917. prom_halt();
  918. }
  919. pbm->num_pbm_ranges =
  920. (err / sizeof(struct linux_prom_pci_ranges));
  921. /* Mask out the top 8 bits of the ranges, leaving the real
  922. * physical address.
  923. */
  924. for (i = 0; i < pbm->num_pbm_ranges; i++)
  925. pbm->pbm_ranges[i].parent_phys_hi &= 0x0fffffff;
  926. pci_sun4v_determine_mem_io_space(pbm);
  927. pbm_register_toplevel_resources(p, pbm);
  928. err = prom_getproperty(prom_node, "interrupt-map",
  929. (char *)pbm->pbm_intmap,
  930. sizeof(pbm->pbm_intmap));
  931. if (err == 0 || err == -1) {
  932. prom_printf("%s: Fatal error, no interrupt-map property.\n",
  933. pbm->name);
  934. prom_halt();
  935. }
  936. pbm->num_pbm_intmap = (err / sizeof(struct linux_prom_pci_intmap));
  937. err = prom_getproperty(prom_node, "interrupt-map-mask",
  938. (char *)&pbm->pbm_intmask,
  939. sizeof(pbm->pbm_intmask));
  940. if (err == 0 || err == -1) {
  941. prom_printf("%s: Fatal error, no interrupt-map-mask.\n",
  942. pbm->name);
  943. prom_halt();
  944. }
  945. pci_sun4v_get_bus_range(pbm);
  946. pci_sun4v_iommu_init(pbm);
  947. pdev_htab_populate(pbm);
  948. }
  949. void sun4v_pci_init(int node, char *model_name)
  950. {
  951. struct pci_controller_info *p;
  952. struct pci_iommu *iommu;
  953. struct linux_prom64_registers regs;
  954. u32 devhandle;
  955. int i;
  956. prom_getproperty(node, "reg", (char *)&regs, sizeof(regs));
  957. devhandle = (regs.phys_addr >> 32UL) & 0x0fffffff;
  958. for (p = pci_controller_root; p; p = p->next) {
  959. struct pci_pbm_info *pbm;
  960. if (p->pbm_A.prom_node && p->pbm_B.prom_node)
  961. continue;
  962. pbm = (p->pbm_A.prom_node ?
  963. &p->pbm_A :
  964. &p->pbm_B);
  965. if (pbm->devhandle == (devhandle ^ 0x40)) {
  966. pci_sun4v_pbm_init(p, node, devhandle);
  967. return;
  968. }
  969. }
  970. for_each_possible_cpu(i) {
  971. unsigned long page = get_zeroed_page(GFP_ATOMIC);
  972. if (!page)
  973. goto fatal_memory_error;
  974. per_cpu(pci_iommu_batch, i).pglist = (u64 *) page;
  975. }
  976. p = kmalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
  977. if (!p)
  978. goto fatal_memory_error;
  979. memset(p, 0, sizeof(*p));
  980. iommu = kmalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
  981. if (!iommu)
  982. goto fatal_memory_error;
  983. memset(iommu, 0, sizeof(*iommu));
  984. p->pbm_A.iommu = iommu;
  985. iommu = kmalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
  986. if (!iommu)
  987. goto fatal_memory_error;
  988. memset(iommu, 0, sizeof(*iommu));
  989. p->pbm_B.iommu = iommu;
  990. p->next = pci_controller_root;
  991. pci_controller_root = p;
  992. p->index = pci_num_controllers++;
  993. p->pbms_same_domain = 0;
  994. p->scan_bus = pci_sun4v_scan_bus;
  995. p->irq_build = pci_sun4v_irq_build;
  996. p->base_address_update = pci_sun4v_base_address_update;
  997. p->resource_adjust = pci_sun4v_resource_adjust;
  998. p->pci_ops = &pci_sun4v_ops;
  999. /* Like PSYCHO and SCHIZO we have a 2GB aligned area
  1000. * for memory space.
  1001. */
  1002. pci_memspace_mask = 0x7fffffffUL;
  1003. pci_sun4v_pbm_init(p, node, devhandle);
  1004. return;
  1005. fatal_memory_error:
  1006. prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");
  1007. prom_halt();
  1008. }