pci.c 22 KB

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