pci-ioda.c 35 KB

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