pci-ioda.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. /*
  2. * Support PCI/PCIe on PowerNV platforms
  3. *
  4. * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #undef DEBUG
  12. #include <linux/kernel.h>
  13. #include <linux/pci.h>
  14. #include <linux/delay.h>
  15. #include <linux/string.h>
  16. #include <linux/init.h>
  17. #include <linux/bootmem.h>
  18. #include <linux/irq.h>
  19. #include <linux/io.h>
  20. #include <linux/msi.h>
  21. #include <asm/sections.h>
  22. #include <asm/io.h>
  23. #include <asm/prom.h>
  24. #include <asm/pci-bridge.h>
  25. #include <asm/machdep.h>
  26. #include <asm/ppc-pci.h>
  27. #include <asm/opal.h>
  28. #include <asm/iommu.h>
  29. #include <asm/tce.h>
  30. #include <asm/abs_addr.h>
  31. #include "powernv.h"
  32. #include "pci.h"
  33. struct resource_wrap {
  34. struct list_head link;
  35. resource_size_t size;
  36. resource_size_t align;
  37. struct pci_dev *dev; /* Set if it's a device */
  38. struct pci_bus *bus; /* Set if it's a bridge */
  39. };
  40. static int __pe_printk(const char *level, const struct pnv_ioda_pe *pe,
  41. struct va_format *vaf)
  42. {
  43. char pfix[32];
  44. if (pe->pdev)
  45. strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
  46. else
  47. sprintf(pfix, "%04x:%02x ",
  48. pci_domain_nr(pe->pbus), pe->pbus->number);
  49. return printk("pci %s%s: [PE# %.3d] %pV", level, pfix, pe->pe_number, vaf);
  50. }
  51. #define define_pe_printk_level(func, kern_level) \
  52. static int func(const struct pnv_ioda_pe *pe, const char *fmt, ...) \
  53. { \
  54. struct va_format vaf; \
  55. va_list args; \
  56. int r; \
  57. \
  58. va_start(args, fmt); \
  59. \
  60. vaf.fmt = fmt; \
  61. vaf.va = &args; \
  62. \
  63. r = __pe_printk(kern_level, pe, &vaf); \
  64. va_end(args); \
  65. \
  66. return r; \
  67. } \
  68. define_pe_printk_level(pe_err, KERN_ERR);
  69. define_pe_printk_level(pe_warn, KERN_WARNING);
  70. define_pe_printk_level(pe_info, KERN_INFO);
  71. /* Calculate resource usage & alignment requirement of a single
  72. * device. This will also assign all resources within the device
  73. * for a given type starting at 0 for the biggest one and then
  74. * assigning in decreasing order of size.
  75. */
  76. static void __devinit pnv_ioda_calc_dev(struct pci_dev *dev, unsigned int flags,
  77. resource_size_t *size,
  78. resource_size_t *align)
  79. {
  80. resource_size_t start;
  81. struct resource *r;
  82. int i;
  83. pr_devel(" -> CDR %s\n", pci_name(dev));
  84. *size = *align = 0;
  85. /* Clear the resources out and mark them all unset */
  86. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  87. r = &dev->resource[i];
  88. if (!(r->flags & flags))
  89. continue;
  90. if (r->start) {
  91. r->end -= r->start;
  92. r->start = 0;
  93. }
  94. r->flags |= IORESOURCE_UNSET;
  95. }
  96. /* We currently keep all memory resources together, we
  97. * will handle prefetch & 64-bit separately in the future
  98. * but for now we stick everybody in M32
  99. */
  100. start = 0;
  101. for (;;) {
  102. resource_size_t max_size = 0;
  103. int max_no = -1;
  104. /* Find next biggest resource */
  105. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  106. r = &dev->resource[i];
  107. if (!(r->flags & IORESOURCE_UNSET) ||
  108. !(r->flags & flags))
  109. continue;
  110. if (resource_size(r) > max_size) {
  111. max_size = resource_size(r);
  112. max_no = i;
  113. }
  114. }
  115. if (max_no < 0)
  116. break;
  117. r = &dev->resource[max_no];
  118. if (max_size > *align)
  119. *align = max_size;
  120. *size += max_size;
  121. r->start = start;
  122. start += max_size;
  123. r->end = r->start + max_size - 1;
  124. r->flags &= ~IORESOURCE_UNSET;
  125. pr_devel(" -> R%d %016llx..%016llx\n",
  126. max_no, r->start, r->end);
  127. }
  128. pr_devel(" <- CDR %s size=%llx align=%llx\n",
  129. pci_name(dev), *size, *align);
  130. }
  131. /* Allocate a resource "wrap" for a given device or bridge and
  132. * insert it at the right position in the sorted list
  133. */
  134. static void __devinit pnv_ioda_add_wrap(struct list_head *list,
  135. struct pci_bus *bus,
  136. struct pci_dev *dev,
  137. resource_size_t size,
  138. resource_size_t align)
  139. {
  140. struct resource_wrap *w1, *w = kzalloc(sizeof(*w), GFP_KERNEL);
  141. w->size = size;
  142. w->align = align;
  143. w->dev = dev;
  144. w->bus = bus;
  145. list_for_each_entry(w1, list, link) {
  146. if (w1->align < align) {
  147. list_add_tail(&w->link, &w1->link);
  148. return;
  149. }
  150. }
  151. list_add_tail(&w->link, list);
  152. }
  153. /* Offset device resources of a given type */
  154. static void __devinit pnv_ioda_offset_dev(struct pci_dev *dev,
  155. unsigned int flags,
  156. resource_size_t offset)
  157. {
  158. struct resource *r;
  159. int i;
  160. pr_devel(" -> ODR %s [%x] +%016llx\n", pci_name(dev), flags, offset);
  161. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  162. r = &dev->resource[i];
  163. if (r->flags & flags) {
  164. dev->resource[i].start += offset;
  165. dev->resource[i].end += offset;
  166. }
  167. }
  168. pr_devel(" <- ODR %s [%x] +%016llx\n", pci_name(dev), flags, offset);
  169. }
  170. /* Offset bus resources (& all children) of a given type */
  171. static void __devinit pnv_ioda_offset_bus(struct pci_bus *bus,
  172. unsigned int flags,
  173. resource_size_t offset)
  174. {
  175. struct resource *r;
  176. struct pci_dev *dev;
  177. struct pci_bus *cbus;
  178. int i;
  179. pr_devel(" -> OBR %s [%x] +%016llx\n",
  180. bus->self ? pci_name(bus->self) : "root", flags, offset);
  181. pci_bus_for_each_resource(bus, r, i) {
  182. if (r && (r->flags & flags)) {
  183. r->start += offset;
  184. r->end += offset;
  185. }
  186. }
  187. list_for_each_entry(dev, &bus->devices, bus_list)
  188. pnv_ioda_offset_dev(dev, flags, offset);
  189. list_for_each_entry(cbus, &bus->children, node)
  190. pnv_ioda_offset_bus(cbus, flags, offset);
  191. pr_devel(" <- OBR %s [%x]\n",
  192. bus->self ? pci_name(bus->self) : "root", flags);
  193. }
  194. /* This is the guts of our IODA resource allocation. This is called
  195. * recursively for each bus in the system. It calculates all the
  196. * necessary size and requirements for children and assign them
  197. * resources such that:
  198. *
  199. * - Each function fits in it's own contiguous set of IO/M32
  200. * segment
  201. *
  202. * - All segments behind a P2P bridge are contiguous and obey
  203. * alignment constraints of those bridges
  204. */
  205. static void __devinit pnv_ioda_calc_bus(struct pci_bus *bus, unsigned int flags,
  206. resource_size_t *size,
  207. resource_size_t *align)
  208. {
  209. struct pci_controller *hose = pci_bus_to_host(bus);
  210. struct pnv_phb *phb = hose->private_data;
  211. resource_size_t dev_size, dev_align, start;
  212. resource_size_t min_align, min_balign;
  213. struct pci_dev *cdev;
  214. struct pci_bus *cbus;
  215. struct list_head head;
  216. struct resource_wrap *w;
  217. unsigned int bres;
  218. *size = *align = 0;
  219. pr_devel("-> CBR %s [%x]\n",
  220. bus->self ? pci_name(bus->self) : "root", flags);
  221. /* Calculate alignment requirements based on the type
  222. * of resource we are working on
  223. */
  224. if (flags & IORESOURCE_IO) {
  225. bres = 0;
  226. min_align = phb->ioda.io_segsize;
  227. min_balign = 0x1000;
  228. } else {
  229. bres = 1;
  230. min_align = phb->ioda.m32_segsize;
  231. min_balign = 0x100000;
  232. }
  233. /* Gather all our children resources ordered by alignment */
  234. INIT_LIST_HEAD(&head);
  235. /* - Busses */
  236. list_for_each_entry(cbus, &bus->children, node) {
  237. pnv_ioda_calc_bus(cbus, flags, &dev_size, &dev_align);
  238. pnv_ioda_add_wrap(&head, cbus, NULL, dev_size, dev_align);
  239. }
  240. /* - Devices */
  241. list_for_each_entry(cdev, &bus->devices, bus_list) {
  242. pnv_ioda_calc_dev(cdev, flags, &dev_size, &dev_align);
  243. /* Align them to segment size */
  244. if (dev_align < min_align)
  245. dev_align = min_align;
  246. pnv_ioda_add_wrap(&head, NULL, cdev, dev_size, dev_align);
  247. }
  248. if (list_empty(&head))
  249. goto empty;
  250. /* Now we can do two things: assign offsets to them within that
  251. * level and get our total alignment & size requirements. The
  252. * assignment algorithm is going to be uber-trivial for now, we
  253. * can try to be smarter later at filling out holes.
  254. */
  255. if (bus->self) {
  256. /* No offset for downstream bridges */
  257. start = 0;
  258. } else {
  259. /* Offset from the root */
  260. if (flags & IORESOURCE_IO)
  261. /* Don't hand out IO 0 */
  262. start = hose->io_resource.start + 0x1000;
  263. else
  264. start = hose->mem_resources[0].start;
  265. }
  266. while(!list_empty(&head)) {
  267. w = list_first_entry(&head, struct resource_wrap, link);
  268. list_del(&w->link);
  269. if (w->size) {
  270. if (start) {
  271. start = ALIGN(start, w->align);
  272. if (w->dev)
  273. pnv_ioda_offset_dev(w->dev,flags,start);
  274. else if (w->bus)
  275. pnv_ioda_offset_bus(w->bus,flags,start);
  276. }
  277. if (w->align > *align)
  278. *align = w->align;
  279. }
  280. start += w->size;
  281. kfree(w);
  282. }
  283. *size = start;
  284. /* Align and setup bridge resources */
  285. *align = max_t(resource_size_t, *align,
  286. max_t(resource_size_t, min_align, min_balign));
  287. *size = ALIGN(*size,
  288. max_t(resource_size_t, min_align, min_balign));
  289. empty:
  290. /* Only setup P2P's, not the PHB itself */
  291. if (bus->self) {
  292. struct resource *res = bus->resource[bres];
  293. if (WARN_ON(res == NULL))
  294. return;
  295. /*
  296. * FIXME: We should probably export and call
  297. * pci_bridge_check_ranges() to properly re-initialize
  298. * the PCI portion of the flags here, and to detect
  299. * what the bridge actually supports.
  300. */
  301. res->start = 0;
  302. res->flags = (*size) ? flags : 0;
  303. res->end = (*size) ? (*size - 1) : 0;
  304. }
  305. pr_devel("<- CBR %s [%x] *size=%016llx *align=%016llx\n",
  306. bus->self ? pci_name(bus->self) : "root", flags,*size,*align);
  307. }
  308. static struct pci_dn *pnv_ioda_get_pdn(struct pci_dev *dev)
  309. {
  310. struct device_node *np;
  311. np = pci_device_to_OF_node(dev);
  312. if (!np)
  313. return NULL;
  314. return PCI_DN(np);
  315. }
  316. static void __devinit pnv_ioda_setup_pe_segments(struct pci_dev *dev)
  317. {
  318. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  319. struct pnv_phb *phb = hose->private_data;
  320. struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
  321. unsigned int pe, i;
  322. resource_size_t pos;
  323. struct resource io_res;
  324. struct resource m32_res;
  325. struct pci_bus_region region;
  326. int rc;
  327. /* Anything not referenced in the device-tree gets PE#0 */
  328. pe = pdn ? pdn->pe_number : 0;
  329. /* Calculate the device min/max */
  330. io_res.start = m32_res.start = (resource_size_t)-1;
  331. io_res.end = m32_res.end = 0;
  332. io_res.flags = IORESOURCE_IO;
  333. m32_res.flags = IORESOURCE_MEM;
  334. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  335. struct resource *r = NULL;
  336. if (dev->resource[i].flags & IORESOURCE_IO)
  337. r = &io_res;
  338. if (dev->resource[i].flags & IORESOURCE_MEM)
  339. r = &m32_res;
  340. if (!r)
  341. continue;
  342. if (dev->resource[i].start < r->start)
  343. r->start = dev->resource[i].start;
  344. if (dev->resource[i].end > r->end)
  345. r->end = dev->resource[i].end;
  346. }
  347. /* Setup IO segments */
  348. if (io_res.start < io_res.end) {
  349. pcibios_resource_to_bus(dev, &region, &io_res);
  350. pos = region.start;
  351. i = pos / phb->ioda.io_segsize;
  352. while(i < phb->ioda.total_pe && pos <= region.end) {
  353. if (phb->ioda.io_segmap[i]) {
  354. pr_err("%s: Trying to use IO seg #%d which is"
  355. " already used by PE# %d\n",
  356. pci_name(dev), i,
  357. phb->ioda.io_segmap[i]);
  358. /* XXX DO SOMETHING TO DISABLE DEVICE ? */
  359. break;
  360. }
  361. phb->ioda.io_segmap[i] = pe;
  362. rc = opal_pci_map_pe_mmio_window(phb->opal_id, pe,
  363. OPAL_IO_WINDOW_TYPE,
  364. 0, i);
  365. if (rc != OPAL_SUCCESS) {
  366. pr_err("%s: OPAL error %d setting up mapping"
  367. " for IO seg# %d\n",
  368. pci_name(dev), rc, i);
  369. /* XXX DO SOMETHING TO DISABLE DEVICE ? */
  370. break;
  371. }
  372. pos += phb->ioda.io_segsize;
  373. i++;
  374. };
  375. }
  376. /* Setup M32 segments */
  377. if (m32_res.start < m32_res.end) {
  378. pcibios_resource_to_bus(dev, &region, &m32_res);
  379. pos = region.start;
  380. i = pos / phb->ioda.m32_segsize;
  381. while(i < phb->ioda.total_pe && pos <= region.end) {
  382. if (phb->ioda.m32_segmap[i]) {
  383. pr_err("%s: Trying to use M32 seg #%d which is"
  384. " already used by PE# %d\n",
  385. pci_name(dev), i,
  386. phb->ioda.m32_segmap[i]);
  387. /* XXX DO SOMETHING TO DISABLE DEVICE ? */
  388. break;
  389. }
  390. phb->ioda.m32_segmap[i] = pe;
  391. rc = opal_pci_map_pe_mmio_window(phb->opal_id, pe,
  392. OPAL_M32_WINDOW_TYPE,
  393. 0, i);
  394. if (rc != OPAL_SUCCESS) {
  395. pr_err("%s: OPAL error %d setting up mapping"
  396. " for M32 seg# %d\n",
  397. pci_name(dev), rc, i);
  398. /* XXX DO SOMETHING TO DISABLE DEVICE ? */
  399. break;
  400. }
  401. pos += phb->ioda.m32_segsize;
  402. i++;
  403. }
  404. }
  405. }
  406. /* Check if a resource still fits in the total IO or M32 range
  407. * for a given PHB
  408. */
  409. static int __devinit pnv_ioda_resource_fit(struct pci_controller *hose,
  410. struct resource *r)
  411. {
  412. struct resource *bounds;
  413. if (r->flags & IORESOURCE_IO)
  414. bounds = &hose->io_resource;
  415. else if (r->flags & IORESOURCE_MEM)
  416. bounds = &hose->mem_resources[0];
  417. else
  418. return 1;
  419. if (r->start >= bounds->start && r->end <= bounds->end)
  420. return 1;
  421. r->flags = 0;
  422. return 0;
  423. }
  424. static void __devinit pnv_ioda_update_resources(struct pci_bus *bus)
  425. {
  426. struct pci_controller *hose = pci_bus_to_host(bus);
  427. struct pci_bus *cbus;
  428. struct pci_dev *cdev;
  429. unsigned int i;
  430. /* We used to clear all device enables here. However it looks like
  431. * clearing MEM enable causes Obsidian (IPR SCS) to go bonkers,
  432. * and shoot fatal errors to the PHB which in turns fences itself
  433. * and we can't recover from that ... yet. So for now, let's leave
  434. * the enables as-is and hope for the best.
  435. */
  436. /* Check if bus resources fit in our IO or M32 range */
  437. for (i = 0; bus->self && (i < 2); i++) {
  438. struct resource *r = bus->resource[i];
  439. if (r && !pnv_ioda_resource_fit(hose, r))
  440. pr_err("%s: Bus %d resource %d disabled, no room\n",
  441. pci_name(bus->self), bus->number, i);
  442. }
  443. /* Update self if it's not a PHB */
  444. if (bus->self)
  445. pci_setup_bridge(bus);
  446. /* Update child devices */
  447. list_for_each_entry(cdev, &bus->devices, bus_list) {
  448. /* Check if resource fits, if not, disabled it */
  449. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  450. struct resource *r = &cdev->resource[i];
  451. if (!pnv_ioda_resource_fit(hose, r))
  452. pr_err("%s: Resource %d disabled, no room\n",
  453. pci_name(cdev), i);
  454. }
  455. /* Assign segments */
  456. pnv_ioda_setup_pe_segments(cdev);
  457. /* Update HW BARs */
  458. for (i = 0; i <= PCI_ROM_RESOURCE; i++)
  459. pci_update_resource(cdev, i);
  460. }
  461. /* Update child busses */
  462. list_for_each_entry(cbus, &bus->children, node)
  463. pnv_ioda_update_resources(cbus);
  464. }
  465. static int __devinit pnv_ioda_alloc_pe(struct pnv_phb *phb)
  466. {
  467. unsigned long pe;
  468. do {
  469. pe = find_next_zero_bit(phb->ioda.pe_alloc,
  470. phb->ioda.total_pe, 0);
  471. if (pe >= phb->ioda.total_pe)
  472. return IODA_INVALID_PE;
  473. } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
  474. phb->ioda.pe_array[pe].pe_number = pe;
  475. return pe;
  476. }
  477. static void __devinit pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
  478. {
  479. WARN_ON(phb->ioda.pe_array[pe].pdev);
  480. memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
  481. clear_bit(pe, phb->ioda.pe_alloc);
  482. }
  483. /* Currently those 2 are only used when MSIs are enabled, this will change
  484. * but in the meantime, we need to protect them to avoid warnings
  485. */
  486. #ifdef CONFIG_PCI_MSI
  487. static struct pnv_ioda_pe * __devinit __pnv_ioda_get_one_pe(struct pci_dev *dev)
  488. {
  489. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  490. struct pnv_phb *phb = hose->private_data;
  491. struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
  492. if (!pdn)
  493. return NULL;
  494. if (pdn->pe_number == IODA_INVALID_PE)
  495. return NULL;
  496. return &phb->ioda.pe_array[pdn->pe_number];
  497. }
  498. static struct pnv_ioda_pe * __devinit pnv_ioda_get_pe(struct pci_dev *dev)
  499. {
  500. struct pnv_ioda_pe *pe = __pnv_ioda_get_one_pe(dev);
  501. while (!pe && dev->bus->self) {
  502. dev = dev->bus->self;
  503. pe = __pnv_ioda_get_one_pe(dev);
  504. if (pe)
  505. pe = pe->bus_pe;
  506. }
  507. return pe;
  508. }
  509. #endif /* CONFIG_PCI_MSI */
  510. static int __devinit pnv_ioda_configure_pe(struct pnv_phb *phb,
  511. struct pnv_ioda_pe *pe)
  512. {
  513. struct pci_dev *parent;
  514. uint8_t bcomp, dcomp, fcomp;
  515. long rc, rid_end, rid;
  516. /* Bus validation ? */
  517. if (pe->pbus) {
  518. int count;
  519. dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
  520. fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
  521. parent = pe->pbus->self;
  522. count = pe->pbus->subordinate - pe->pbus->secondary + 1;
  523. switch(count) {
  524. case 1: bcomp = OpalPciBusAll; break;
  525. case 2: bcomp = OpalPciBus7Bits; break;
  526. case 4: bcomp = OpalPciBus6Bits; break;
  527. case 8: bcomp = OpalPciBus5Bits; break;
  528. case 16: bcomp = OpalPciBus4Bits; break;
  529. case 32: bcomp = OpalPciBus3Bits; break;
  530. default:
  531. pr_err("%s: Number of subordinate busses %d"
  532. " unsupported\n",
  533. pci_name(pe->pbus->self), count);
  534. /* Do an exact match only */
  535. bcomp = OpalPciBusAll;
  536. }
  537. rid_end = pe->rid + (count << 8);
  538. } else {
  539. parent = pe->pdev->bus->self;
  540. bcomp = OpalPciBusAll;
  541. dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
  542. fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
  543. rid_end = pe->rid + 1;
  544. }
  545. /* Associate PE in PELT */
  546. rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
  547. bcomp, dcomp, fcomp, OPAL_MAP_PE);
  548. if (rc) {
  549. pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
  550. return -ENXIO;
  551. }
  552. opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
  553. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  554. /* Add to all parents PELT-V */
  555. while (parent) {
  556. struct pci_dn *pdn = pnv_ioda_get_pdn(parent);
  557. if (pdn && pdn->pe_number != IODA_INVALID_PE) {
  558. rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
  559. pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
  560. /* XXX What to do in case of error ? */
  561. }
  562. parent = parent->bus->self;
  563. }
  564. /* Setup reverse map */
  565. for (rid = pe->rid; rid < rid_end; rid++)
  566. phb->ioda.pe_rmap[rid] = pe->pe_number;
  567. /* Setup one MVTs on IODA1 */
  568. if (phb->type == PNV_PHB_IODA1) {
  569. pe->mve_number = pe->pe_number;
  570. rc = opal_pci_set_mve(phb->opal_id, pe->mve_number,
  571. pe->pe_number);
  572. if (rc) {
  573. pe_err(pe, "OPAL error %ld setting up MVE %d\n",
  574. rc, pe->mve_number);
  575. pe->mve_number = -1;
  576. } else {
  577. rc = opal_pci_set_mve_enable(phb->opal_id,
  578. pe->mve_number, OPAL_ENABLE_MVE);
  579. if (rc) {
  580. pe_err(pe, "OPAL error %ld enabling MVE %d\n",
  581. rc, pe->mve_number);
  582. pe->mve_number = -1;
  583. }
  584. }
  585. } else if (phb->type == PNV_PHB_IODA2)
  586. pe->mve_number = 0;
  587. return 0;
  588. }
  589. static void __devinit pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
  590. struct pnv_ioda_pe *pe)
  591. {
  592. struct pnv_ioda_pe *lpe;
  593. list_for_each_entry(lpe, &phb->ioda.pe_list, link) {
  594. if (lpe->dma_weight < pe->dma_weight) {
  595. list_add_tail(&pe->link, &lpe->link);
  596. return;
  597. }
  598. }
  599. list_add_tail(&pe->link, &phb->ioda.pe_list);
  600. }
  601. static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
  602. {
  603. /* This is quite simplistic. The "base" weight of a device
  604. * is 10. 0 means no DMA is to be accounted for it.
  605. */
  606. /* If it's a bridge, no DMA */
  607. if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
  608. return 0;
  609. /* Reduce the weight of slow USB controllers */
  610. if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
  611. dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
  612. dev->class == PCI_CLASS_SERIAL_USB_EHCI)
  613. return 3;
  614. /* Increase the weight of RAID (includes Obsidian) */
  615. if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
  616. return 15;
  617. /* Default */
  618. return 10;
  619. }
  620. static struct pnv_ioda_pe * __devinit pnv_ioda_setup_dev_PE(struct pci_dev *dev)
  621. {
  622. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  623. struct pnv_phb *phb = hose->private_data;
  624. struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
  625. struct pnv_ioda_pe *pe;
  626. int pe_num;
  627. if (!pdn) {
  628. pr_err("%s: Device tree node not associated properly\n",
  629. pci_name(dev));
  630. return NULL;
  631. }
  632. if (pdn->pe_number != IODA_INVALID_PE)
  633. return NULL;
  634. /* PE#0 has been pre-set */
  635. if (dev->bus->number == 0)
  636. pe_num = 0;
  637. else
  638. pe_num = pnv_ioda_alloc_pe(phb);
  639. if (pe_num == IODA_INVALID_PE) {
  640. pr_warning("%s: Not enough PE# available, disabling device\n",
  641. pci_name(dev));
  642. return NULL;
  643. }
  644. /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
  645. * pointer in the PE data structure, both should be destroyed at the
  646. * same time. However, this needs to be looked at more closely again
  647. * once we actually start removing things (Hotplug, SR-IOV, ...)
  648. *
  649. * At some point we want to remove the PDN completely anyways
  650. */
  651. pe = &phb->ioda.pe_array[pe_num];
  652. pci_dev_get(dev);
  653. pdn->pcidev = dev;
  654. pdn->pe_number = pe_num;
  655. pe->pdev = dev;
  656. pe->pbus = NULL;
  657. pe->tce32_seg = -1;
  658. pe->mve_number = -1;
  659. pe->rid = dev->bus->number << 8 | pdn->devfn;
  660. pe_info(pe, "Associated device to PE\n");
  661. if (pnv_ioda_configure_pe(phb, pe)) {
  662. /* XXX What do we do here ? */
  663. if (pe_num)
  664. pnv_ioda_free_pe(phb, pe_num);
  665. pdn->pe_number = IODA_INVALID_PE;
  666. pe->pdev = NULL;
  667. pci_dev_put(dev);
  668. return NULL;
  669. }
  670. /* Assign a DMA weight to the device */
  671. pe->dma_weight = pnv_ioda_dma_weight(dev);
  672. if (pe->dma_weight != 0) {
  673. phb->ioda.dma_weight += pe->dma_weight;
  674. phb->ioda.dma_pe_count++;
  675. }
  676. /* Link the PE */
  677. pnv_ioda_link_pe_by_weight(phb, pe);
  678. return pe;
  679. }
  680. static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
  681. {
  682. struct pci_dev *dev;
  683. list_for_each_entry(dev, &bus->devices, bus_list) {
  684. struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
  685. if (pdn == NULL) {
  686. pr_warn("%s: No device node associated with device !\n",
  687. pci_name(dev));
  688. continue;
  689. }
  690. pci_dev_get(dev);
  691. pdn->pcidev = dev;
  692. pdn->pe_number = pe->pe_number;
  693. pe->dma_weight += pnv_ioda_dma_weight(dev);
  694. if (dev->subordinate)
  695. pnv_ioda_setup_same_PE(dev->subordinate, pe);
  696. }
  697. }
  698. static void __devinit pnv_ioda_setup_bus_PE(struct pci_dev *dev,
  699. struct pnv_ioda_pe *ppe)
  700. {
  701. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  702. struct pnv_phb *phb = hose->private_data;
  703. struct pci_bus *bus = dev->subordinate;
  704. struct pnv_ioda_pe *pe;
  705. int pe_num;
  706. if (!bus) {
  707. pr_warning("%s: Bridge without a subordinate bus !\n",
  708. pci_name(dev));
  709. return;
  710. }
  711. pe_num = pnv_ioda_alloc_pe(phb);
  712. if (pe_num == IODA_INVALID_PE) {
  713. pr_warning("%s: Not enough PE# available, disabling bus\n",
  714. pci_name(dev));
  715. return;
  716. }
  717. pe = &phb->ioda.pe_array[pe_num];
  718. ppe->bus_pe = pe;
  719. pe->pbus = bus;
  720. pe->pdev = NULL;
  721. pe->tce32_seg = -1;
  722. pe->mve_number = -1;
  723. pe->rid = bus->secondary << 8;
  724. pe->dma_weight = 0;
  725. pe_info(pe, "Secondary busses %d..%d associated with PE\n",
  726. bus->secondary, bus->subordinate);
  727. if (pnv_ioda_configure_pe(phb, pe)) {
  728. /* XXX What do we do here ? */
  729. if (pe_num)
  730. pnv_ioda_free_pe(phb, pe_num);
  731. pe->pbus = NULL;
  732. return;
  733. }
  734. /* Associate it with all child devices */
  735. pnv_ioda_setup_same_PE(bus, pe);
  736. /* Account for one DMA PE if at least one DMA capable device exist
  737. * below the bridge
  738. */
  739. if (pe->dma_weight != 0) {
  740. phb->ioda.dma_weight += pe->dma_weight;
  741. phb->ioda.dma_pe_count++;
  742. }
  743. /* Link the PE */
  744. pnv_ioda_link_pe_by_weight(phb, pe);
  745. }
  746. static void __devinit pnv_ioda_setup_PEs(struct pci_bus *bus)
  747. {
  748. struct pci_dev *dev;
  749. struct pnv_ioda_pe *pe;
  750. list_for_each_entry(dev, &bus->devices, bus_list) {
  751. pe = pnv_ioda_setup_dev_PE(dev);
  752. if (pe == NULL)
  753. continue;
  754. /* Leaving the PCIe domain ... single PE# */
  755. if (dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
  756. pnv_ioda_setup_bus_PE(dev, pe);
  757. else if (dev->subordinate)
  758. pnv_ioda_setup_PEs(dev->subordinate);
  759. }
  760. }
  761. static void __devinit pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb,
  762. struct pci_dev *dev)
  763. {
  764. /* We delay DMA setup after we have assigned all PE# */
  765. }
  766. static void __devinit pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
  767. struct pci_bus *bus)
  768. {
  769. struct pci_dev *dev;
  770. list_for_each_entry(dev, &bus->devices, bus_list) {
  771. set_iommu_table_base(&dev->dev, &pe->tce32_table);
  772. if (dev->subordinate)
  773. pnv_ioda_setup_bus_dma(pe, dev->subordinate);
  774. }
  775. }
  776. static void __devinit pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
  777. struct pnv_ioda_pe *pe,
  778. unsigned int base,
  779. unsigned int segs)
  780. {
  781. struct page *tce_mem = NULL;
  782. const __be64 *swinvp;
  783. struct iommu_table *tbl;
  784. unsigned int i;
  785. int64_t rc;
  786. void *addr;
  787. /* 256M DMA window, 4K TCE pages, 8 bytes TCE */
  788. #define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
  789. /* XXX FIXME: Handle 64-bit only DMA devices */
  790. /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
  791. /* XXX FIXME: Allocate multi-level tables on PHB3 */
  792. /* We shouldn't already have a 32-bit DMA associated */
  793. if (WARN_ON(pe->tce32_seg >= 0))
  794. return;
  795. /* Grab a 32-bit TCE table */
  796. pe->tce32_seg = base;
  797. pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
  798. (base << 28), ((base + segs) << 28) - 1);
  799. /* XXX Currently, we allocate one big contiguous table for the
  800. * TCEs. We only really need one chunk per 256M of TCE space
  801. * (ie per segment) but that's an optimization for later, it
  802. * requires some added smarts with our get/put_tce implementation
  803. */
  804. tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
  805. get_order(TCE32_TABLE_SIZE * segs));
  806. if (!tce_mem) {
  807. pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
  808. goto fail;
  809. }
  810. addr = page_address(tce_mem);
  811. memset(addr, 0, TCE32_TABLE_SIZE * segs);
  812. /* Configure HW */
  813. for (i = 0; i < segs; i++) {
  814. rc = opal_pci_map_pe_dma_window(phb->opal_id,
  815. pe->pe_number,
  816. base + i, 1,
  817. __pa(addr) + TCE32_TABLE_SIZE * i,
  818. TCE32_TABLE_SIZE, 0x1000);
  819. if (rc) {
  820. pe_err(pe, " Failed to configure 32-bit TCE table,"
  821. " err %ld\n", rc);
  822. goto fail;
  823. }
  824. }
  825. /* Setup linux iommu table */
  826. tbl = &pe->tce32_table;
  827. pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
  828. base << 28);
  829. /* OPAL variant of P7IOC SW invalidated TCEs */
  830. swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
  831. if (swinvp) {
  832. /* We need a couple more fields -- an address and a data
  833. * to or. Since the bus is only printed out on table free
  834. * errors, and on the first pass the data will be a relative
  835. * bus number, print that out instead.
  836. */
  837. tbl->it_busno = 0;
  838. tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8);
  839. tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE
  840. | TCE_PCI_SWINV_PAIR;
  841. }
  842. iommu_init_table(tbl, phb->hose->node);
  843. if (pe->pdev)
  844. set_iommu_table_base(&pe->pdev->dev, tbl);
  845. else
  846. pnv_ioda_setup_bus_dma(pe, pe->pbus);
  847. return;
  848. fail:
  849. /* XXX Failure: Try to fallback to 64-bit only ? */
  850. if (pe->tce32_seg >= 0)
  851. pe->tce32_seg = -1;
  852. if (tce_mem)
  853. __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
  854. }
  855. static void __devinit pnv_ioda_setup_dma(struct pnv_phb *phb)
  856. {
  857. struct pci_controller *hose = phb->hose;
  858. unsigned int residual, remaining, segs, tw, base;
  859. struct pnv_ioda_pe *pe;
  860. /* If we have more PE# than segments available, hand out one
  861. * per PE until we run out and let the rest fail. If not,
  862. * then we assign at least one segment per PE, plus more based
  863. * on the amount of devices under that PE
  864. */
  865. if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
  866. residual = 0;
  867. else
  868. residual = phb->ioda.tce32_count -
  869. phb->ioda.dma_pe_count;
  870. pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
  871. hose->global_number, phb->ioda.tce32_count);
  872. pr_info("PCI: %d PE# for a total weight of %d\n",
  873. phb->ioda.dma_pe_count, phb->ioda.dma_weight);
  874. /* Walk our PE list and configure their DMA segments, hand them
  875. * out one base segment plus any residual segments based on
  876. * weight
  877. */
  878. remaining = phb->ioda.tce32_count;
  879. tw = phb->ioda.dma_weight;
  880. base = 0;
  881. list_for_each_entry(pe, &phb->ioda.pe_list, link) {
  882. if (!pe->dma_weight)
  883. continue;
  884. if (!remaining) {
  885. pe_warn(pe, "No DMA32 resources available\n");
  886. continue;
  887. }
  888. segs = 1;
  889. if (residual) {
  890. segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
  891. if (segs > remaining)
  892. segs = remaining;
  893. }
  894. pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
  895. pe->dma_weight, segs);
  896. pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
  897. remaining -= segs;
  898. base += segs;
  899. }
  900. }
  901. #ifdef CONFIG_PCI_MSI
  902. static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
  903. unsigned int hwirq, unsigned int is_64,
  904. struct msi_msg *msg)
  905. {
  906. struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
  907. unsigned int xive_num = hwirq - phb->msi_base;
  908. uint64_t addr64;
  909. uint32_t addr32, data;
  910. int rc;
  911. /* No PE assigned ? bail out ... no MSI for you ! */
  912. if (pe == NULL)
  913. return -ENXIO;
  914. /* Check if we have an MVE */
  915. if (pe->mve_number < 0)
  916. return -ENXIO;
  917. /* Assign XIVE to PE */
  918. rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
  919. if (rc) {
  920. pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
  921. pci_name(dev), rc, xive_num);
  922. return -EIO;
  923. }
  924. if (is_64) {
  925. rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
  926. &addr64, &data);
  927. if (rc) {
  928. pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
  929. pci_name(dev), rc);
  930. return -EIO;
  931. }
  932. msg->address_hi = addr64 >> 32;
  933. msg->address_lo = addr64 & 0xfffffffful;
  934. } else {
  935. rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
  936. &addr32, &data);
  937. if (rc) {
  938. pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
  939. pci_name(dev), rc);
  940. return -EIO;
  941. }
  942. msg->address_hi = 0;
  943. msg->address_lo = addr32;
  944. }
  945. msg->data = data;
  946. pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
  947. " address=%x_%08x data=%x PE# %d\n",
  948. pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
  949. msg->address_hi, msg->address_lo, data, pe->pe_number);
  950. return 0;
  951. }
  952. static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
  953. {
  954. unsigned int bmap_size;
  955. const __be32 *prop = of_get_property(phb->hose->dn,
  956. "ibm,opal-msi-ranges", NULL);
  957. if (!prop) {
  958. /* BML Fallback */
  959. prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
  960. }
  961. if (!prop)
  962. return;
  963. phb->msi_base = be32_to_cpup(prop);
  964. phb->msi_count = be32_to_cpup(prop + 1);
  965. bmap_size = BITS_TO_LONGS(phb->msi_count) * sizeof(unsigned long);
  966. phb->msi_map = zalloc_maybe_bootmem(bmap_size, GFP_KERNEL);
  967. if (!phb->msi_map) {
  968. pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
  969. phb->hose->global_number);
  970. return;
  971. }
  972. phb->msi_setup = pnv_pci_ioda_msi_setup;
  973. phb->msi32_support = 1;
  974. pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
  975. phb->msi_count, phb->msi_base);
  976. }
  977. #else
  978. static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
  979. #endif /* CONFIG_PCI_MSI */
  980. /* This is the starting point of our IODA specific resource
  981. * allocation process
  982. */
  983. static void __devinit pnv_pci_ioda_fixup_phb(struct pci_controller *hose)
  984. {
  985. resource_size_t size, align;
  986. struct pci_bus *child;
  987. /* Associate PEs per functions */
  988. pnv_ioda_setup_PEs(hose->bus);
  989. /* Calculate all resources */
  990. pnv_ioda_calc_bus(hose->bus, IORESOURCE_IO, &size, &align);
  991. pnv_ioda_calc_bus(hose->bus, IORESOURCE_MEM, &size, &align);
  992. /* Apply then to HW */
  993. pnv_ioda_update_resources(hose->bus);
  994. /* Setup DMA */
  995. pnv_ioda_setup_dma(hose->private_data);
  996. /* Configure PCI Express settings */
  997. list_for_each_entry(child, &hose->bus->children, node) {
  998. struct pci_dev *self = child->self;
  999. if (!self)
  1000. continue;
  1001. pcie_bus_configure_settings(child, self->pcie_mpss);
  1002. }
  1003. }
  1004. /* Prevent enabling devices for which we couldn't properly
  1005. * assign a PE
  1006. */
  1007. static int __devinit pnv_pci_enable_device_hook(struct pci_dev *dev)
  1008. {
  1009. struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
  1010. if (!pdn || pdn->pe_number == IODA_INVALID_PE)
  1011. return -EINVAL;
  1012. return 0;
  1013. }
  1014. static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
  1015. u32 devfn)
  1016. {
  1017. return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
  1018. }
  1019. void __init pnv_pci_init_ioda1_phb(struct device_node *np)
  1020. {
  1021. struct pci_controller *hose;
  1022. static int primary = 1;
  1023. struct pnv_phb *phb;
  1024. unsigned long size, m32map_off, iomap_off, pemap_off;
  1025. const u64 *prop64;
  1026. u64 phb_id;
  1027. void *aux;
  1028. long rc;
  1029. pr_info(" Initializing IODA OPAL PHB %s\n", np->full_name);
  1030. prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
  1031. if (!prop64) {
  1032. pr_err(" Missing \"ibm,opal-phbid\" property !\n");
  1033. return;
  1034. }
  1035. phb_id = be64_to_cpup(prop64);
  1036. pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
  1037. phb = alloc_bootmem(sizeof(struct pnv_phb));
  1038. if (phb) {
  1039. memset(phb, 0, sizeof(struct pnv_phb));
  1040. phb->hose = hose = pcibios_alloc_controller(np);
  1041. }
  1042. if (!phb || !phb->hose) {
  1043. pr_err("PCI: Failed to allocate PCI controller for %s\n",
  1044. np->full_name);
  1045. return;
  1046. }
  1047. spin_lock_init(&phb->lock);
  1048. /* XXX Use device-tree */
  1049. hose->first_busno = 0;
  1050. hose->last_busno = 0xff;
  1051. hose->private_data = phb;
  1052. phb->opal_id = phb_id;
  1053. phb->type = PNV_PHB_IODA1;
  1054. /* Detect specific models for error handling */
  1055. if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
  1056. phb->model = PNV_PHB_MODEL_P7IOC;
  1057. else
  1058. phb->model = PNV_PHB_MODEL_UNKNOWN;
  1059. /* We parse "ranges" now since we need to deduce the register base
  1060. * from the IO base
  1061. */
  1062. pci_process_bridge_OF_ranges(phb->hose, np, primary);
  1063. primary = 0;
  1064. /* Magic formula from Milton */
  1065. phb->regs = of_iomap(np, 0);
  1066. if (phb->regs == NULL)
  1067. pr_err(" Failed to map registers !\n");
  1068. /* XXX This is hack-a-thon. This needs to be changed so that:
  1069. * - we obtain stuff like PE# etc... from device-tree
  1070. * - we properly re-allocate M32 ourselves
  1071. * (the OFW one isn't very good)
  1072. */
  1073. /* Initialize more IODA stuff */
  1074. phb->ioda.total_pe = 128;
  1075. phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
  1076. /* OFW Has already off top 64k of M32 space (MSI space) */
  1077. phb->ioda.m32_size += 0x10000;
  1078. phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
  1079. phb->ioda.m32_pci_base = hose->mem_resources[0].start -
  1080. hose->pci_mem_offset;
  1081. phb->ioda.io_size = hose->pci_io_size;
  1082. phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
  1083. phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
  1084. /* Allocate aux data & arrays */
  1085. size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
  1086. m32map_off = size;
  1087. size += phb->ioda.total_pe;
  1088. iomap_off = size;
  1089. size += phb->ioda.total_pe;
  1090. pemap_off = size;
  1091. size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
  1092. aux = alloc_bootmem(size);
  1093. memset(aux, 0, size);
  1094. phb->ioda.pe_alloc = aux;
  1095. phb->ioda.m32_segmap = aux + m32map_off;
  1096. phb->ioda.io_segmap = aux + iomap_off;
  1097. phb->ioda.pe_array = aux + pemap_off;
  1098. set_bit(0, phb->ioda.pe_alloc);
  1099. INIT_LIST_HEAD(&phb->ioda.pe_list);
  1100. /* Calculate how many 32-bit TCE segments we have */
  1101. phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
  1102. /* Clear unusable m64 */
  1103. hose->mem_resources[1].flags = 0;
  1104. hose->mem_resources[1].start = 0;
  1105. hose->mem_resources[1].end = 0;
  1106. hose->mem_resources[2].flags = 0;
  1107. hose->mem_resources[2].start = 0;
  1108. hose->mem_resources[2].end = 0;
  1109. #if 0
  1110. rc = opal_pci_set_phb_mem_window(opal->phb_id,
  1111. window_type,
  1112. window_num,
  1113. starting_real_address,
  1114. starting_pci_address,
  1115. segment_size);
  1116. #endif
  1117. pr_info(" %d PE's M32: 0x%x [segment=0x%x] IO: 0x%x [segment=0x%x]\n",
  1118. phb->ioda.total_pe,
  1119. phb->ioda.m32_size, phb->ioda.m32_segsize,
  1120. phb->ioda.io_size, phb->ioda.io_segsize);
  1121. if (phb->regs) {
  1122. pr_devel(" BUID = 0x%016llx\n", in_be64(phb->regs + 0x100));
  1123. pr_devel(" PHB2_CR = 0x%016llx\n", in_be64(phb->regs + 0x160));
  1124. pr_devel(" IO_BAR = 0x%016llx\n", in_be64(phb->regs + 0x170));
  1125. pr_devel(" IO_BAMR = 0x%016llx\n", in_be64(phb->regs + 0x178));
  1126. pr_devel(" IO_SAR = 0x%016llx\n", in_be64(phb->regs + 0x180));
  1127. pr_devel(" M32_BAR = 0x%016llx\n", in_be64(phb->regs + 0x190));
  1128. pr_devel(" M32_BAMR = 0x%016llx\n", in_be64(phb->regs + 0x198));
  1129. pr_devel(" M32_SAR = 0x%016llx\n", in_be64(phb->regs + 0x1a0));
  1130. }
  1131. phb->hose->ops = &pnv_pci_ops;
  1132. /* Setup RID -> PE mapping function */
  1133. phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
  1134. /* Setup TCEs */
  1135. phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
  1136. /* Setup MSI support */
  1137. pnv_pci_init_ioda_msis(phb);
  1138. /* We set both PCI_PROBE_ONLY and PCI_REASSIGN_ALL_RSRC. This is an
  1139. * odd combination which essentially means that we skip all resource
  1140. * fixups and assignments in the generic code, and do it all
  1141. * ourselves here
  1142. */
  1143. ppc_md.pcibios_fixup_phb = pnv_pci_ioda_fixup_phb;
  1144. ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
  1145. pci_add_flags(PCI_PROBE_ONLY | PCI_REASSIGN_ALL_RSRC);
  1146. /* Reset IODA tables to a clean state */
  1147. rc = opal_pci_reset(phb_id, OPAL_PCI_IODA_TABLE_RESET, OPAL_ASSERT_RESET);
  1148. if (rc)
  1149. pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
  1150. opal_pci_set_pe(phb_id, 0, 0, 7, 1, 1 , OPAL_MAP_PE);
  1151. }
  1152. void __init pnv_pci_init_ioda_hub(struct device_node *np)
  1153. {
  1154. struct device_node *phbn;
  1155. const u64 *prop64;
  1156. u64 hub_id;
  1157. pr_info("Probing IODA IO-Hub %s\n", np->full_name);
  1158. prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
  1159. if (!prop64) {
  1160. pr_err(" Missing \"ibm,opal-hubid\" property !\n");
  1161. return;
  1162. }
  1163. hub_id = be64_to_cpup(prop64);
  1164. pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
  1165. /* Count child PHBs */
  1166. for_each_child_of_node(np, phbn) {
  1167. /* Look for IODA1 PHBs */
  1168. if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
  1169. pnv_pci_init_ioda1_phb(phbn);
  1170. }
  1171. }