pci-ioda.c 40 KB

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