pci.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. /*
  2. * Copyright (C) 2001 Allan Trautman, IBM Corporation
  3. * Copyright (C) 2005,2007 Stephen Rothwell, IBM Corp
  4. *
  5. * iSeries specific routines for PCI.
  6. *
  7. * Based on code from pci.c and iSeries_pci.c 32bit
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/list.h>
  25. #include <linux/string.h>
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <asm/types.h>
  30. #include <asm/io.h>
  31. #include <asm/irq.h>
  32. #include <asm/prom.h>
  33. #include <asm/machdep.h>
  34. #include <asm/pci-bridge.h>
  35. #include <asm/iommu.h>
  36. #include <asm/abs_addr.h>
  37. #include <asm/firmware.h>
  38. #include <asm/iseries/hv_types.h>
  39. #include <asm/iseries/hv_call_xm.h>
  40. #include <asm/iseries/mf.h>
  41. #include <asm/iseries/iommu.h>
  42. #include <asm/ppc-pci.h>
  43. #include "irq.h"
  44. #include "pci.h"
  45. #include "call_pci.h"
  46. #define PCI_RETRY_MAX 3
  47. static int limit_pci_retries = 1; /* Set Retry Error on. */
  48. /*
  49. * Table defines
  50. * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
  51. */
  52. #define IOMM_TABLE_MAX_ENTRIES 1024
  53. #define IOMM_TABLE_ENTRY_SIZE 0x0000000000400000UL
  54. #define BASE_IO_MEMORY 0xE000000000000000UL
  55. static unsigned long max_io_memory = BASE_IO_MEMORY;
  56. static long current_iomm_table_entry;
  57. /*
  58. * Lookup Tables.
  59. */
  60. static struct device_node *iomm_table[IOMM_TABLE_MAX_ENTRIES];
  61. static u8 iobar_table[IOMM_TABLE_MAX_ENTRIES];
  62. static const char pci_io_text[] = "iSeries PCI I/O";
  63. static DEFINE_SPINLOCK(iomm_table_lock);
  64. /*
  65. * Generate a Direct Select Address for the Hypervisor
  66. */
  67. static inline u64 iseries_ds_addr(struct device_node *node)
  68. {
  69. struct pci_dn *pdn = PCI_DN(node);
  70. return ((u64)pdn->busno << 48) + ((u64)pdn->bussubno << 40)
  71. + ((u64)0x10 << 32);
  72. }
  73. /*
  74. * Size of Bus VPD data
  75. */
  76. #define BUS_VPDSIZE 1024
  77. /*
  78. * Bus Vpd Tags
  79. */
  80. #define VPD_END_OF_AREA 0x79
  81. #define VPD_ID_STRING 0x82
  82. #define VPD_VENDOR_AREA 0x84
  83. /*
  84. * Mfg Area Tags
  85. */
  86. #define VPD_FRU_FRAME_ID 0x4649 /* "FI" */
  87. #define VPD_SLOT_MAP_FORMAT 0x4D46 /* "MF" */
  88. #define VPD_SLOT_MAP 0x534D /* "SM" */
  89. /*
  90. * Structures of the areas
  91. */
  92. struct mfg_vpd_area {
  93. u16 tag;
  94. u8 length;
  95. u8 data1;
  96. u8 data2;
  97. };
  98. #define MFG_ENTRY_SIZE 3
  99. struct slot_map {
  100. u8 agent;
  101. u8 secondary_agent;
  102. u8 phb;
  103. char card_location[3];
  104. char parms[8];
  105. char reserved[2];
  106. };
  107. #define SLOT_ENTRY_SIZE 16
  108. /*
  109. * Parse the Slot Area
  110. */
  111. static void __init iseries_parse_slot_area(struct slot_map *map, int len,
  112. HvAgentId agent, u8 *phb, char card[4])
  113. {
  114. /*
  115. * Parse Slot label until we find the one requested
  116. */
  117. while (len > 0) {
  118. if (map->agent == agent) {
  119. /*
  120. * If Phb wasn't found, grab the entry first one found.
  121. */
  122. if (*phb == 0xff)
  123. *phb = map->phb;
  124. /* Found it, extract the data. */
  125. if (map->phb == *phb) {
  126. memcpy(card, &map->card_location, 3);
  127. card[3] = 0;
  128. break;
  129. }
  130. }
  131. /* Point to the next Slot */
  132. map = (struct slot_map *)((char *)map + SLOT_ENTRY_SIZE);
  133. len -= SLOT_ENTRY_SIZE;
  134. }
  135. }
  136. /*
  137. * Parse the Mfg Area
  138. */
  139. static void __init iseries_parse_mfg_area(struct mfg_vpd_area *area, int len,
  140. HvAgentId agent, u8 *phb, u8 *frame, char card[4])
  141. {
  142. u16 slot_map_fmt = 0;
  143. /* Parse Mfg Data */
  144. while (len > 0) {
  145. int mfg_tag_len = area->length;
  146. /* Frame ID (FI 4649020310 ) */
  147. if (area->tag == VPD_FRU_FRAME_ID)
  148. *frame = area->data1;
  149. /* Slot Map Format (MF 4D46020004 ) */
  150. else if (area->tag == VPD_SLOT_MAP_FORMAT)
  151. slot_map_fmt = (area->data1 * 256)
  152. + area->data2;
  153. /* Slot Map (SM 534D90 */
  154. else if (area->tag == VPD_SLOT_MAP) {
  155. struct slot_map *slot_map;
  156. if (slot_map_fmt == 0x1004)
  157. slot_map = (struct slot_map *)((char *)area
  158. + MFG_ENTRY_SIZE + 1);
  159. else
  160. slot_map = (struct slot_map *)((char *)area
  161. + MFG_ENTRY_SIZE);
  162. iseries_parse_slot_area(slot_map, mfg_tag_len,
  163. agent, phb, card);
  164. }
  165. /*
  166. * Point to the next Mfg Area
  167. * Use defined size, sizeof give wrong answer
  168. */
  169. area = (struct mfg_vpd_area *)((char *)area + mfg_tag_len
  170. + MFG_ENTRY_SIZE);
  171. len -= (mfg_tag_len + MFG_ENTRY_SIZE);
  172. }
  173. }
  174. /*
  175. * Look for "BUS".. Data is not Null terminated.
  176. * PHBID of 0xFF indicates PHB was not found in VPD Data.
  177. */
  178. static u8 __init iseries_parse_phbid(u8 *area, int len)
  179. {
  180. while (len > 0) {
  181. if ((*area == 'B') && (*(area + 1) == 'U')
  182. && (*(area + 2) == 'S')) {
  183. area += 3;
  184. while (*area == ' ')
  185. area++;
  186. return *area & 0x0F;
  187. }
  188. area++;
  189. len--;
  190. }
  191. return 0xff;
  192. }
  193. /*
  194. * Parse out the VPD Areas
  195. */
  196. static void __init iseries_parse_vpd(u8 *data, int data_len,
  197. HvAgentId agent, u8 *frame, char card[4])
  198. {
  199. u8 phb = 0xff;
  200. while (data_len > 0) {
  201. int len;
  202. u8 tag = *data;
  203. if (tag == VPD_END_OF_AREA)
  204. break;
  205. len = *(data + 1) + (*(data + 2) * 256);
  206. data += 3;
  207. data_len -= 3;
  208. if (tag == VPD_ID_STRING)
  209. phb = iseries_parse_phbid(data, len);
  210. else if (tag == VPD_VENDOR_AREA)
  211. iseries_parse_mfg_area((struct mfg_vpd_area *)data, len,
  212. agent, &phb, frame, card);
  213. /* Point to next Area. */
  214. data += len;
  215. data_len -= len;
  216. }
  217. }
  218. static int __init iseries_get_location_code(u16 bus, HvAgentId agent,
  219. u8 *frame, char card[4])
  220. {
  221. int status = 0;
  222. int bus_vpd_len = 0;
  223. u8 *bus_vpd = kmalloc(BUS_VPDSIZE, GFP_KERNEL);
  224. if (bus_vpd == NULL) {
  225. printk("PCI: Bus VPD Buffer allocation failure.\n");
  226. return 0;
  227. }
  228. bus_vpd_len = HvCallPci_getBusVpd(bus, iseries_hv_addr(bus_vpd),
  229. BUS_VPDSIZE);
  230. if (bus_vpd_len == 0) {
  231. printk("PCI: Bus VPD Buffer zero length.\n");
  232. goto out_free;
  233. }
  234. /* printk("PCI: bus_vpd: %p, %d\n",bus_vpd, bus_vpd_len); */
  235. /* Make sure this is what I think it is */
  236. if (*bus_vpd != VPD_ID_STRING) {
  237. printk("PCI: Bus VPD Buffer missing starting tag.\n");
  238. goto out_free;
  239. }
  240. iseries_parse_vpd(bus_vpd, bus_vpd_len, agent, frame, card);
  241. status = 1;
  242. out_free:
  243. kfree(bus_vpd);
  244. return status;
  245. }
  246. /*
  247. * Prints the device information.
  248. * - Pass in pci_dev* pointer to the device.
  249. * - Pass in the device count
  250. *
  251. * Format:
  252. * PCI: Bus 0, Device 26, Vendor 0x12AE Frame 1, Card C10 Ethernet
  253. * controller
  254. */
  255. static void __init iseries_device_information(struct pci_dev *pdev, int count,
  256. u16 bus, HvSubBusNumber subbus)
  257. {
  258. u8 frame = 0;
  259. char card[4];
  260. HvAgentId agent;
  261. agent = ISERIES_PCI_AGENTID(ISERIES_GET_DEVICE_FROM_SUBBUS(subbus),
  262. ISERIES_GET_FUNCTION_FROM_SUBBUS(subbus));
  263. if (iseries_get_location_code(bus, agent, &frame, card)) {
  264. printk("%d. PCI: Bus%3d, Device%3d, Vendor %04X Frame%3d, "
  265. "Card %4s 0x%04X\n", count, bus,
  266. PCI_SLOT(pdev->devfn), pdev->vendor, frame,
  267. card, (int)(pdev->class >> 8));
  268. }
  269. }
  270. /*
  271. * iomm_table_allocate_entry
  272. *
  273. * Adds pci_dev entry in address translation table
  274. *
  275. * - Allocates the number of entries required in table base on BAR
  276. * size.
  277. * - Allocates starting at BASE_IO_MEMORY and increases.
  278. * - The size is round up to be a multiple of entry size.
  279. * - CurrentIndex is incremented to keep track of the last entry.
  280. * - Builds the resource entry for allocated BARs.
  281. */
  282. static void __init iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
  283. {
  284. struct resource *bar_res = &dev->resource[bar_num];
  285. long bar_size = pci_resource_len(dev, bar_num);
  286. /*
  287. * No space to allocate, quick exit, skip Allocation.
  288. */
  289. if (bar_size == 0)
  290. return;
  291. /*
  292. * Set Resource values.
  293. */
  294. spin_lock(&iomm_table_lock);
  295. bar_res->name = pci_io_text;
  296. bar_res->start = BASE_IO_MEMORY +
  297. IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
  298. bar_res->end = bar_res->start + bar_size - 1;
  299. /*
  300. * Allocate the number of table entries needed for BAR.
  301. */
  302. while (bar_size > 0 ) {
  303. iomm_table[current_iomm_table_entry] = dev->sysdata;
  304. iobar_table[current_iomm_table_entry] = bar_num;
  305. bar_size -= IOMM_TABLE_ENTRY_SIZE;
  306. ++current_iomm_table_entry;
  307. }
  308. max_io_memory = BASE_IO_MEMORY +
  309. IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
  310. spin_unlock(&iomm_table_lock);
  311. }
  312. /*
  313. * allocate_device_bars
  314. *
  315. * - Allocates ALL pci_dev BAR's and updates the resources with the
  316. * BAR value. BARS with zero length will have the resources
  317. * The HvCallPci_getBarParms is used to get the size of the BAR
  318. * space. It calls iomm_table_allocate_entry to allocate
  319. * each entry.
  320. * - Loops through The Bar resources(0 - 5) including the ROM
  321. * is resource(6).
  322. */
  323. static void __init allocate_device_bars(struct pci_dev *dev)
  324. {
  325. int bar_num;
  326. for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num)
  327. iomm_table_allocate_entry(dev, bar_num);
  328. }
  329. /*
  330. * Log error information to system console.
  331. * Filter out the device not there errors.
  332. * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
  333. * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
  334. * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
  335. */
  336. static void pci_log_error(char *error, int bus, int subbus,
  337. int agent, int hv_res)
  338. {
  339. if (hv_res == 0x0302)
  340. return;
  341. printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
  342. error, bus, subbus, agent, hv_res);
  343. }
  344. /*
  345. * Look down the chain to find the matching Device Device
  346. */
  347. static struct device_node *find_device_node(int bus, int devfn)
  348. {
  349. struct device_node *node;
  350. for (node = NULL; (node = of_find_all_nodes(node)); ) {
  351. struct pci_dn *pdn = PCI_DN(node);
  352. if (pdn && (bus == pdn->busno) && (devfn == pdn->devfn))
  353. return node;
  354. }
  355. return NULL;
  356. }
  357. /*
  358. * iSeries_pci_final_fixup(void)
  359. */
  360. void __init iSeries_pci_final_fixup(void)
  361. {
  362. struct pci_dev *pdev = NULL;
  363. struct device_node *node;
  364. int num_dev = 0;
  365. /* Fix up at the device node and pci_dev relationship */
  366. mf_display_src(0xC9000100);
  367. printk("pcibios_final_fixup\n");
  368. for_each_pci_dev(pdev) {
  369. const u32 *agent;
  370. const u32 *sub_bus;
  371. unsigned char bus = pdev->bus->number;
  372. node = find_device_node(bus, pdev->devfn);
  373. printk("pci dev %p (%x.%x), node %p\n", pdev, bus,
  374. pdev->devfn, node);
  375. if (!node) {
  376. printk("PCI: Device Tree not found for 0x%016lX\n",
  377. (unsigned long)pdev);
  378. continue;
  379. }
  380. agent = of_get_property(node, "linux,agent-id", NULL);
  381. sub_bus = of_get_property(node, "linux,subbus", NULL);
  382. if (agent && sub_bus) {
  383. u8 irq = iSeries_allocate_IRQ(bus, 0, *sub_bus);
  384. int err;
  385. err = HvCallXm_connectBusUnit(bus, *sub_bus,
  386. *agent, irq);
  387. if (err)
  388. pci_log_error("Connect Bus Unit",
  389. bus, *sub_bus, *agent, err);
  390. else {
  391. err = HvCallPci_configStore8(bus, *sub_bus,
  392. *agent, PCI_INTERRUPT_LINE, irq);
  393. if (err)
  394. pci_log_error("PciCfgStore Irq Failed!",
  395. bus, *sub_bus, *agent, err);
  396. else
  397. pdev->irq = irq;
  398. }
  399. }
  400. num_dev++;
  401. pdev->sysdata = node;
  402. PCI_DN(node)->pcidev = pdev;
  403. allocate_device_bars(pdev);
  404. iseries_device_information(pdev, num_dev, bus, *sub_bus);
  405. iommu_devnode_init_iSeries(pdev, node);
  406. }
  407. iSeries_activate_IRQs();
  408. mf_display_src(0xC9000200);
  409. }
  410. /*
  411. * Config space read and write functions.
  412. * For now at least, we look for the device node for the bus and devfn
  413. * that we are asked to access. It may be possible to translate the devfn
  414. * to a subbus and deviceid more directly.
  415. */
  416. static u64 hv_cfg_read_func[4] = {
  417. HvCallPciConfigLoad8, HvCallPciConfigLoad16,
  418. HvCallPciConfigLoad32, HvCallPciConfigLoad32
  419. };
  420. static u64 hv_cfg_write_func[4] = {
  421. HvCallPciConfigStore8, HvCallPciConfigStore16,
  422. HvCallPciConfigStore32, HvCallPciConfigStore32
  423. };
  424. /*
  425. * Read PCI config space
  426. */
  427. static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
  428. int offset, int size, u32 *val)
  429. {
  430. struct device_node *node = find_device_node(bus->number, devfn);
  431. u64 fn;
  432. struct HvCallPci_LoadReturn ret;
  433. if (node == NULL)
  434. return PCIBIOS_DEVICE_NOT_FOUND;
  435. if (offset > 255) {
  436. *val = ~0;
  437. return PCIBIOS_BAD_REGISTER_NUMBER;
  438. }
  439. fn = hv_cfg_read_func[(size - 1) & 3];
  440. HvCall3Ret16(fn, &ret, iseries_ds_addr(node), offset, 0);
  441. if (ret.rc != 0) {
  442. *val = ~0;
  443. return PCIBIOS_DEVICE_NOT_FOUND; /* or something */
  444. }
  445. *val = ret.value;
  446. return 0;
  447. }
  448. /*
  449. * Write PCI config space
  450. */
  451. static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
  452. int offset, int size, u32 val)
  453. {
  454. struct device_node *node = find_device_node(bus->number, devfn);
  455. u64 fn;
  456. u64 ret;
  457. if (node == NULL)
  458. return PCIBIOS_DEVICE_NOT_FOUND;
  459. if (offset > 255)
  460. return PCIBIOS_BAD_REGISTER_NUMBER;
  461. fn = hv_cfg_write_func[(size - 1) & 3];
  462. ret = HvCall4(fn, iseries_ds_addr(node), offset, val, 0);
  463. if (ret != 0)
  464. return PCIBIOS_DEVICE_NOT_FOUND;
  465. return 0;
  466. }
  467. static struct pci_ops iSeries_pci_ops = {
  468. .read = iSeries_pci_read_config,
  469. .write = iSeries_pci_write_config
  470. };
  471. /*
  472. * Check Return Code
  473. * -> On Failure, print and log information.
  474. * Increment Retry Count, if exceeds max, panic partition.
  475. *
  476. * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
  477. * PCI: Device 23.90 ReadL Retry( 1)
  478. * PCI: Device 23.90 ReadL Retry Successful(1)
  479. */
  480. static int check_return_code(char *type, struct device_node *dn,
  481. int *retry, u64 ret)
  482. {
  483. if (ret != 0) {
  484. struct pci_dn *pdn = PCI_DN(dn);
  485. (*retry)++;
  486. printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
  487. type, pdn->busno, pdn->devfn,
  488. *retry, (int)ret);
  489. /*
  490. * Bump the retry and check for retry count exceeded.
  491. * If, Exceeded, panic the system.
  492. */
  493. if (((*retry) > PCI_RETRY_MAX) &&
  494. (limit_pci_retries > 0)) {
  495. mf_display_src(0xB6000103);
  496. panic_timeout = 0;
  497. panic("PCI: Hardware I/O Error, SRC B6000103, "
  498. "Automatic Reboot Disabled.\n");
  499. }
  500. return -1; /* Retry Try */
  501. }
  502. return 0;
  503. }
  504. /*
  505. * Translate the I/O Address into a device node, bar, and bar offset.
  506. * Note: Make sure the passed variable end up on the stack to avoid
  507. * the exposure of being device global.
  508. */
  509. static inline struct device_node *xlate_iomm_address(
  510. const volatile void __iomem *addr,
  511. u64 *dsaptr, u64 *bar_offset, const char *func)
  512. {
  513. unsigned long orig_addr;
  514. unsigned long base_addr;
  515. unsigned long ind;
  516. struct device_node *dn;
  517. orig_addr = (unsigned long __force)addr;
  518. if ((orig_addr < BASE_IO_MEMORY) || (orig_addr >= max_io_memory)) {
  519. static unsigned long last_jiffies;
  520. static int num_printed;
  521. if ((jiffies - last_jiffies) > 60 * HZ) {
  522. last_jiffies = jiffies;
  523. num_printed = 0;
  524. }
  525. if (num_printed++ < 10)
  526. printk(KERN_ERR
  527. "iSeries_%s: invalid access at IO address %p\n",
  528. func, addr);
  529. return NULL;
  530. }
  531. base_addr = orig_addr - BASE_IO_MEMORY;
  532. ind = base_addr / IOMM_TABLE_ENTRY_SIZE;
  533. dn = iomm_table[ind];
  534. if (dn != NULL) {
  535. int barnum = iobar_table[ind];
  536. *dsaptr = iseries_ds_addr(dn) | (barnum << 24);
  537. *bar_offset = base_addr % IOMM_TABLE_ENTRY_SIZE;
  538. } else
  539. panic("PCI: Invalid PCI IO address detected!\n");
  540. return dn;
  541. }
  542. /*
  543. * Read MM I/O Instructions for the iSeries
  544. * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
  545. * else, data is returned in Big Endian format.
  546. */
  547. static u8 iseries_readb(const volatile void __iomem *addr)
  548. {
  549. u64 bar_offset;
  550. u64 dsa;
  551. int retry = 0;
  552. struct HvCallPci_LoadReturn ret;
  553. struct device_node *dn =
  554. xlate_iomm_address(addr, &dsa, &bar_offset, "read_byte");
  555. if (dn == NULL)
  556. return 0xff;
  557. do {
  558. HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, bar_offset, 0);
  559. } while (check_return_code("RDB", dn, &retry, ret.rc) != 0);
  560. return ret.value;
  561. }
  562. static u16 iseries_readw_be(const volatile void __iomem *addr)
  563. {
  564. u64 bar_offset;
  565. u64 dsa;
  566. int retry = 0;
  567. struct HvCallPci_LoadReturn ret;
  568. struct device_node *dn =
  569. xlate_iomm_address(addr, &dsa, &bar_offset, "read_word");
  570. if (dn == NULL)
  571. return 0xffff;
  572. do {
  573. HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
  574. bar_offset, 0);
  575. } while (check_return_code("RDW", dn, &retry, ret.rc) != 0);
  576. return ret.value;
  577. }
  578. static u32 iseries_readl_be(const volatile void __iomem *addr)
  579. {
  580. u64 bar_offset;
  581. u64 dsa;
  582. int retry = 0;
  583. struct HvCallPci_LoadReturn ret;
  584. struct device_node *dn =
  585. xlate_iomm_address(addr, &dsa, &bar_offset, "read_long");
  586. if (dn == NULL)
  587. return 0xffffffff;
  588. do {
  589. HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
  590. bar_offset, 0);
  591. } while (check_return_code("RDL", dn, &retry, ret.rc) != 0);
  592. return ret.value;
  593. }
  594. /*
  595. * Write MM I/O Instructions for the iSeries
  596. *
  597. */
  598. static void iseries_writeb(u8 data, volatile void __iomem *addr)
  599. {
  600. u64 bar_offset;
  601. u64 dsa;
  602. int retry = 0;
  603. u64 rc;
  604. struct device_node *dn =
  605. xlate_iomm_address(addr, &dsa, &bar_offset, "write_byte");
  606. if (dn == NULL)
  607. return;
  608. do {
  609. rc = HvCall4(HvCallPciBarStore8, dsa, bar_offset, data, 0);
  610. } while (check_return_code("WWB", dn, &retry, rc) != 0);
  611. }
  612. static void iseries_writew_be(u16 data, volatile void __iomem *addr)
  613. {
  614. u64 bar_offset;
  615. u64 dsa;
  616. int retry = 0;
  617. u64 rc;
  618. struct device_node *dn =
  619. xlate_iomm_address(addr, &dsa, &bar_offset, "write_word");
  620. if (dn == NULL)
  621. return;
  622. do {
  623. rc = HvCall4(HvCallPciBarStore16, dsa, bar_offset, data, 0);
  624. } while (check_return_code("WWW", dn, &retry, rc) != 0);
  625. }
  626. static void iseries_writel_be(u32 data, volatile void __iomem *addr)
  627. {
  628. u64 bar_offset;
  629. u64 dsa;
  630. int retry = 0;
  631. u64 rc;
  632. struct device_node *dn =
  633. xlate_iomm_address(addr, &dsa, &bar_offset, "write_long");
  634. if (dn == NULL)
  635. return;
  636. do {
  637. rc = HvCall4(HvCallPciBarStore32, dsa, bar_offset, data, 0);
  638. } while (check_return_code("WWL", dn, &retry, rc) != 0);
  639. }
  640. static u16 iseries_readw(const volatile void __iomem *addr)
  641. {
  642. return le16_to_cpu(iseries_readw_be(addr));
  643. }
  644. static u32 iseries_readl(const volatile void __iomem *addr)
  645. {
  646. return le32_to_cpu(iseries_readl_be(addr));
  647. }
  648. static void iseries_writew(u16 data, volatile void __iomem *addr)
  649. {
  650. iseries_writew_be(cpu_to_le16(data), addr);
  651. }
  652. static void iseries_writel(u32 data, volatile void __iomem *addr)
  653. {
  654. iseries_writel(cpu_to_le32(data), addr);
  655. }
  656. static void iseries_readsb(const volatile void __iomem *addr, void *buf,
  657. unsigned long count)
  658. {
  659. u8 *dst = buf;
  660. while(count-- > 0)
  661. *(dst++) = iseries_readb(addr);
  662. }
  663. static void iseries_readsw(const volatile void __iomem *addr, void *buf,
  664. unsigned long count)
  665. {
  666. u16 *dst = buf;
  667. while(count-- > 0)
  668. *(dst++) = iseries_readw_be(addr);
  669. }
  670. static void iseries_readsl(const volatile void __iomem *addr, void *buf,
  671. unsigned long count)
  672. {
  673. u32 *dst = buf;
  674. while(count-- > 0)
  675. *(dst++) = iseries_readl_be(addr);
  676. }
  677. static void iseries_writesb(volatile void __iomem *addr, const void *buf,
  678. unsigned long count)
  679. {
  680. const u8 *src = buf;
  681. while(count-- > 0)
  682. iseries_writeb(*(src++), addr);
  683. }
  684. static void iseries_writesw(volatile void __iomem *addr, const void *buf,
  685. unsigned long count)
  686. {
  687. const u16 *src = buf;
  688. while(count-- > 0)
  689. iseries_writew_be(*(src++), addr);
  690. }
  691. static void iseries_writesl(volatile void __iomem *addr, const void *buf,
  692. unsigned long count)
  693. {
  694. const u32 *src = buf;
  695. while(count-- > 0)
  696. iseries_writel_be(*(src++), addr);
  697. }
  698. static void iseries_memset_io(volatile void __iomem *addr, int c,
  699. unsigned long n)
  700. {
  701. volatile char __iomem *d = addr;
  702. while (n-- > 0)
  703. iseries_writeb(c, d++);
  704. }
  705. static void iseries_memcpy_fromio(void *dest, const volatile void __iomem *src,
  706. unsigned long n)
  707. {
  708. char *d = dest;
  709. const volatile char __iomem *s = src;
  710. while (n-- > 0)
  711. *d++ = iseries_readb(s++);
  712. }
  713. static void iseries_memcpy_toio(volatile void __iomem *dest, const void *src,
  714. unsigned long n)
  715. {
  716. const char *s = src;
  717. volatile char __iomem *d = dest;
  718. while (n-- > 0)
  719. iseries_writeb(*s++, d++);
  720. }
  721. /* We only set MMIO ops. The default PIO ops will be default
  722. * to the MMIO ops + pci_io_base which is 0 on iSeries as
  723. * expected so both should work.
  724. *
  725. * Note that we don't implement the readq/writeq versions as
  726. * I don't know of an HV call for doing so. Thus, the default
  727. * operation will be used instead, which will fault a the value
  728. * return by iSeries for MMIO addresses always hits a non mapped
  729. * area. This is as good as the BUG() we used to have there.
  730. */
  731. static struct ppc_pci_io __initdata iseries_pci_io = {
  732. .readb = iseries_readb,
  733. .readw = iseries_readw,
  734. .readl = iseries_readl,
  735. .readw_be = iseries_readw_be,
  736. .readl_be = iseries_readl_be,
  737. .writeb = iseries_writeb,
  738. .writew = iseries_writew,
  739. .writel = iseries_writel,
  740. .writew_be = iseries_writew_be,
  741. .writel_be = iseries_writel_be,
  742. .readsb = iseries_readsb,
  743. .readsw = iseries_readsw,
  744. .readsl = iseries_readsl,
  745. .writesb = iseries_writesb,
  746. .writesw = iseries_writesw,
  747. .writesl = iseries_writesl,
  748. .memset_io = iseries_memset_io,
  749. .memcpy_fromio = iseries_memcpy_fromio,
  750. .memcpy_toio = iseries_memcpy_toio,
  751. };
  752. /*
  753. * iSeries_pcibios_init
  754. *
  755. * Description:
  756. * This function checks for all possible system PCI host bridges that connect
  757. * PCI buses. The system hypervisor is queried as to the guest partition
  758. * ownership status. A pci_controller is built for any bus which is partially
  759. * owned or fully owned by this guest partition.
  760. */
  761. void __init iSeries_pcibios_init(void)
  762. {
  763. struct pci_controller *phb;
  764. struct device_node *root = of_find_node_by_path("/");
  765. struct device_node *node = NULL;
  766. /* Install IO hooks */
  767. ppc_pci_io = iseries_pci_io;
  768. pci_probe_only = 1;
  769. /* iSeries has no IO space in the common sense, it needs to set
  770. * the IO base to 0
  771. */
  772. pci_io_base = 0;
  773. if (root == NULL) {
  774. printk(KERN_CRIT "iSeries_pcibios_init: can't find root "
  775. "of device tree\n");
  776. return;
  777. }
  778. while ((node = of_get_next_child(root, node)) != NULL) {
  779. HvBusNumber bus;
  780. const u32 *busp;
  781. if ((node->type == NULL) || (strcmp(node->type, "pci") != 0))
  782. continue;
  783. busp = of_get_property(node, "bus-range", NULL);
  784. if (busp == NULL)
  785. continue;
  786. bus = *busp;
  787. printk("bus %d appears to exist\n", bus);
  788. phb = pcibios_alloc_controller(node);
  789. if (phb == NULL)
  790. continue;
  791. /* All legacy iSeries PHBs are in domain zero */
  792. phb->global_number = 0;
  793. phb->pci_mem_offset = bus;
  794. phb->first_busno = bus;
  795. phb->last_busno = bus;
  796. phb->ops = &iSeries_pci_ops;
  797. }
  798. of_node_put(root);
  799. pci_devs_phb_init();
  800. }