pci.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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. if (likely(sub_bus))
  406. iseries_device_information(pdev, bus, *sub_bus);
  407. else
  408. printk(KERN_ERR "PCI: Device node %s has missing or invalid "
  409. "linux,subbus property\n", node->full_name);
  410. }
  411. /*
  412. * iSeries_pci_final_fixup(void)
  413. */
  414. void __init iSeries_pci_final_fixup(void)
  415. {
  416. /* Fix up at the device node and pci_dev relationship */
  417. mf_display_src(0xC9000100);
  418. iSeries_activate_IRQs();
  419. mf_display_src(0xC9000200);
  420. }
  421. /*
  422. * Config space read and write functions.
  423. * For now at least, we look for the device node for the bus and devfn
  424. * that we are asked to access. It may be possible to translate the devfn
  425. * to a subbus and deviceid more directly.
  426. */
  427. static u64 hv_cfg_read_func[4] = {
  428. HvCallPciConfigLoad8, HvCallPciConfigLoad16,
  429. HvCallPciConfigLoad32, HvCallPciConfigLoad32
  430. };
  431. static u64 hv_cfg_write_func[4] = {
  432. HvCallPciConfigStore8, HvCallPciConfigStore16,
  433. HvCallPciConfigStore32, HvCallPciConfigStore32
  434. };
  435. /*
  436. * Read PCI config space
  437. */
  438. static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
  439. int offset, int size, u32 *val)
  440. {
  441. struct device_node *node = find_device_node(bus->number, devfn);
  442. u64 fn;
  443. struct HvCallPci_LoadReturn ret;
  444. if (node == NULL)
  445. return PCIBIOS_DEVICE_NOT_FOUND;
  446. if (offset > 255) {
  447. *val = ~0;
  448. return PCIBIOS_BAD_REGISTER_NUMBER;
  449. }
  450. fn = hv_cfg_read_func[(size - 1) & 3];
  451. HvCall3Ret16(fn, &ret, iseries_ds_addr(node), offset, 0);
  452. if (ret.rc != 0) {
  453. *val = ~0;
  454. return PCIBIOS_DEVICE_NOT_FOUND; /* or something */
  455. }
  456. *val = ret.value;
  457. return 0;
  458. }
  459. /*
  460. * Write PCI config space
  461. */
  462. static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
  463. int offset, int size, u32 val)
  464. {
  465. struct device_node *node = find_device_node(bus->number, devfn);
  466. u64 fn;
  467. u64 ret;
  468. if (node == NULL)
  469. return PCIBIOS_DEVICE_NOT_FOUND;
  470. if (offset > 255)
  471. return PCIBIOS_BAD_REGISTER_NUMBER;
  472. fn = hv_cfg_write_func[(size - 1) & 3];
  473. ret = HvCall4(fn, iseries_ds_addr(node), offset, val, 0);
  474. if (ret != 0)
  475. return PCIBIOS_DEVICE_NOT_FOUND;
  476. return 0;
  477. }
  478. static struct pci_ops iSeries_pci_ops = {
  479. .read = iSeries_pci_read_config,
  480. .write = iSeries_pci_write_config
  481. };
  482. /*
  483. * Check Return Code
  484. * -> On Failure, print and log information.
  485. * Increment Retry Count, if exceeds max, panic partition.
  486. *
  487. * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
  488. * PCI: Device 23.90 ReadL Retry( 1)
  489. * PCI: Device 23.90 ReadL Retry Successful(1)
  490. */
  491. static int check_return_code(char *type, struct device_node *dn,
  492. int *retry, u64 ret)
  493. {
  494. if (ret != 0) {
  495. struct pci_dn *pdn = PCI_DN(dn);
  496. (*retry)++;
  497. printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
  498. type, pdn->busno, pdn->devfn,
  499. *retry, (int)ret);
  500. /*
  501. * Bump the retry and check for retry count exceeded.
  502. * If, Exceeded, panic the system.
  503. */
  504. if (((*retry) > PCI_RETRY_MAX) &&
  505. (limit_pci_retries > 0)) {
  506. mf_display_src(0xB6000103);
  507. panic_timeout = 0;
  508. panic("PCI: Hardware I/O Error, SRC B6000103, "
  509. "Automatic Reboot Disabled.\n");
  510. }
  511. return -1; /* Retry Try */
  512. }
  513. return 0;
  514. }
  515. /*
  516. * Translate the I/O Address into a device node, bar, and bar offset.
  517. * Note: Make sure the passed variable end up on the stack to avoid
  518. * the exposure of being device global.
  519. */
  520. static inline struct device_node *xlate_iomm_address(
  521. const volatile void __iomem *addr,
  522. u64 *dsaptr, u64 *bar_offset, const char *func)
  523. {
  524. unsigned long orig_addr;
  525. unsigned long base_addr;
  526. unsigned long ind;
  527. struct device_node *dn;
  528. orig_addr = (unsigned long __force)addr;
  529. if ((orig_addr < BASE_IO_MEMORY) || (orig_addr >= max_io_memory)) {
  530. static DEFINE_RATELIMIT_STATE(ratelimit, 60 * HZ, 10);
  531. if (__ratelimit(&ratelimit))
  532. printk(KERN_ERR
  533. "iSeries_%s: invalid access at IO address %p\n",
  534. func, addr);
  535. return NULL;
  536. }
  537. base_addr = orig_addr - BASE_IO_MEMORY;
  538. ind = base_addr / IOMM_TABLE_ENTRY_SIZE;
  539. dn = iomm_table[ind];
  540. if (dn != NULL) {
  541. *dsaptr = ds_addr_table[ind];
  542. *bar_offset = base_addr % IOMM_TABLE_ENTRY_SIZE;
  543. } else
  544. panic("PCI: Invalid PCI IO address detected!\n");
  545. return dn;
  546. }
  547. /*
  548. * Read MM I/O Instructions for the iSeries
  549. * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
  550. * else, data is returned in Big Endian format.
  551. */
  552. static u8 iseries_readb(const volatile void __iomem *addr)
  553. {
  554. u64 bar_offset;
  555. u64 dsa;
  556. int retry = 0;
  557. struct HvCallPci_LoadReturn ret;
  558. struct device_node *dn =
  559. xlate_iomm_address(addr, &dsa, &bar_offset, "read_byte");
  560. if (dn == NULL)
  561. return 0xff;
  562. do {
  563. HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, bar_offset, 0);
  564. } while (check_return_code("RDB", dn, &retry, ret.rc) != 0);
  565. return ret.value;
  566. }
  567. static u16 iseries_readw_be(const volatile void __iomem *addr)
  568. {
  569. u64 bar_offset;
  570. u64 dsa;
  571. int retry = 0;
  572. struct HvCallPci_LoadReturn ret;
  573. struct device_node *dn =
  574. xlate_iomm_address(addr, &dsa, &bar_offset, "read_word");
  575. if (dn == NULL)
  576. return 0xffff;
  577. do {
  578. HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
  579. bar_offset, 0);
  580. } while (check_return_code("RDW", dn, &retry, ret.rc) != 0);
  581. return ret.value;
  582. }
  583. static u32 iseries_readl_be(const volatile void __iomem *addr)
  584. {
  585. u64 bar_offset;
  586. u64 dsa;
  587. int retry = 0;
  588. struct HvCallPci_LoadReturn ret;
  589. struct device_node *dn =
  590. xlate_iomm_address(addr, &dsa, &bar_offset, "read_long");
  591. if (dn == NULL)
  592. return 0xffffffff;
  593. do {
  594. HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
  595. bar_offset, 0);
  596. } while (check_return_code("RDL", dn, &retry, ret.rc) != 0);
  597. return ret.value;
  598. }
  599. /*
  600. * Write MM I/O Instructions for the iSeries
  601. *
  602. */
  603. static void iseries_writeb(u8 data, volatile void __iomem *addr)
  604. {
  605. u64 bar_offset;
  606. u64 dsa;
  607. int retry = 0;
  608. u64 rc;
  609. struct device_node *dn =
  610. xlate_iomm_address(addr, &dsa, &bar_offset, "write_byte");
  611. if (dn == NULL)
  612. return;
  613. do {
  614. rc = HvCall4(HvCallPciBarStore8, dsa, bar_offset, data, 0);
  615. } while (check_return_code("WWB", dn, &retry, rc) != 0);
  616. }
  617. static void iseries_writew_be(u16 data, volatile void __iomem *addr)
  618. {
  619. u64 bar_offset;
  620. u64 dsa;
  621. int retry = 0;
  622. u64 rc;
  623. struct device_node *dn =
  624. xlate_iomm_address(addr, &dsa, &bar_offset, "write_word");
  625. if (dn == NULL)
  626. return;
  627. do {
  628. rc = HvCall4(HvCallPciBarStore16, dsa, bar_offset, data, 0);
  629. } while (check_return_code("WWW", dn, &retry, rc) != 0);
  630. }
  631. static void iseries_writel_be(u32 data, volatile void __iomem *addr)
  632. {
  633. u64 bar_offset;
  634. u64 dsa;
  635. int retry = 0;
  636. u64 rc;
  637. struct device_node *dn =
  638. xlate_iomm_address(addr, &dsa, &bar_offset, "write_long");
  639. if (dn == NULL)
  640. return;
  641. do {
  642. rc = HvCall4(HvCallPciBarStore32, dsa, bar_offset, data, 0);
  643. } while (check_return_code("WWL", dn, &retry, rc) != 0);
  644. }
  645. static u16 iseries_readw(const volatile void __iomem *addr)
  646. {
  647. return le16_to_cpu(iseries_readw_be(addr));
  648. }
  649. static u32 iseries_readl(const volatile void __iomem *addr)
  650. {
  651. return le32_to_cpu(iseries_readl_be(addr));
  652. }
  653. static void iseries_writew(u16 data, volatile void __iomem *addr)
  654. {
  655. iseries_writew_be(cpu_to_le16(data), addr);
  656. }
  657. static void iseries_writel(u32 data, volatile void __iomem *addr)
  658. {
  659. iseries_writel(cpu_to_le32(data), addr);
  660. }
  661. static void iseries_readsb(const volatile void __iomem *addr, void *buf,
  662. unsigned long count)
  663. {
  664. u8 *dst = buf;
  665. while(count-- > 0)
  666. *(dst++) = iseries_readb(addr);
  667. }
  668. static void iseries_readsw(const volatile void __iomem *addr, void *buf,
  669. unsigned long count)
  670. {
  671. u16 *dst = buf;
  672. while(count-- > 0)
  673. *(dst++) = iseries_readw_be(addr);
  674. }
  675. static void iseries_readsl(const volatile void __iomem *addr, void *buf,
  676. unsigned long count)
  677. {
  678. u32 *dst = buf;
  679. while(count-- > 0)
  680. *(dst++) = iseries_readl_be(addr);
  681. }
  682. static void iseries_writesb(volatile void __iomem *addr, const void *buf,
  683. unsigned long count)
  684. {
  685. const u8 *src = buf;
  686. while(count-- > 0)
  687. iseries_writeb(*(src++), addr);
  688. }
  689. static void iseries_writesw(volatile void __iomem *addr, const void *buf,
  690. unsigned long count)
  691. {
  692. const u16 *src = buf;
  693. while(count-- > 0)
  694. iseries_writew_be(*(src++), addr);
  695. }
  696. static void iseries_writesl(volatile void __iomem *addr, const void *buf,
  697. unsigned long count)
  698. {
  699. const u32 *src = buf;
  700. while(count-- > 0)
  701. iseries_writel_be(*(src++), addr);
  702. }
  703. static void iseries_memset_io(volatile void __iomem *addr, int c,
  704. unsigned long n)
  705. {
  706. volatile char __iomem *d = addr;
  707. while (n-- > 0)
  708. iseries_writeb(c, d++);
  709. }
  710. static void iseries_memcpy_fromio(void *dest, const volatile void __iomem *src,
  711. unsigned long n)
  712. {
  713. char *d = dest;
  714. const volatile char __iomem *s = src;
  715. while (n-- > 0)
  716. *d++ = iseries_readb(s++);
  717. }
  718. static void iseries_memcpy_toio(volatile void __iomem *dest, const void *src,
  719. unsigned long n)
  720. {
  721. const char *s = src;
  722. volatile char __iomem *d = dest;
  723. while (n-- > 0)
  724. iseries_writeb(*s++, d++);
  725. }
  726. /* We only set MMIO ops. The default PIO ops will be default
  727. * to the MMIO ops + pci_io_base which is 0 on iSeries as
  728. * expected so both should work.
  729. *
  730. * Note that we don't implement the readq/writeq versions as
  731. * I don't know of an HV call for doing so. Thus, the default
  732. * operation will be used instead, which will fault a the value
  733. * return by iSeries for MMIO addresses always hits a non mapped
  734. * area. This is as good as the BUG() we used to have there.
  735. */
  736. static struct ppc_pci_io __initdata iseries_pci_io = {
  737. .readb = iseries_readb,
  738. .readw = iseries_readw,
  739. .readl = iseries_readl,
  740. .readw_be = iseries_readw_be,
  741. .readl_be = iseries_readl_be,
  742. .writeb = iseries_writeb,
  743. .writew = iseries_writew,
  744. .writel = iseries_writel,
  745. .writew_be = iseries_writew_be,
  746. .writel_be = iseries_writel_be,
  747. .readsb = iseries_readsb,
  748. .readsw = iseries_readsw,
  749. .readsl = iseries_readsl,
  750. .writesb = iseries_writesb,
  751. .writesw = iseries_writesw,
  752. .writesl = iseries_writesl,
  753. .memset_io = iseries_memset_io,
  754. .memcpy_fromio = iseries_memcpy_fromio,
  755. .memcpy_toio = iseries_memcpy_toio,
  756. };
  757. /*
  758. * iSeries_pcibios_init
  759. *
  760. * Description:
  761. * This function checks for all possible system PCI host bridges that connect
  762. * PCI buses. The system hypervisor is queried as to the guest partition
  763. * ownership status. A pci_controller is built for any bus which is partially
  764. * owned or fully owned by this guest partition.
  765. */
  766. void __init iSeries_pcibios_init(void)
  767. {
  768. struct pci_controller *phb;
  769. struct device_node *root = of_find_node_by_path("/");
  770. struct device_node *node = NULL;
  771. /* Install IO hooks */
  772. ppc_pci_io = iseries_pci_io;
  773. pci_probe_only = 1;
  774. /* iSeries has no IO space in the common sense, it needs to set
  775. * the IO base to 0
  776. */
  777. pci_io_base = 0;
  778. if (root == NULL) {
  779. printk(KERN_CRIT "iSeries_pcibios_init: can't find root "
  780. "of device tree\n");
  781. return;
  782. }
  783. while ((node = of_get_next_child(root, node)) != NULL) {
  784. HvBusNumber bus;
  785. const u32 *busp;
  786. if ((node->type == NULL) || (strcmp(node->type, "pci") != 0))
  787. continue;
  788. busp = of_get_property(node, "bus-range", NULL);
  789. if (busp == NULL)
  790. continue;
  791. bus = *busp;
  792. printk("bus %d appears to exist\n", bus);
  793. phb = pcibios_alloc_controller(node);
  794. if (phb == NULL)
  795. continue;
  796. /* All legacy iSeries PHBs are in domain zero */
  797. phb->global_number = 0;
  798. phb->first_busno = bus;
  799. phb->last_busno = bus;
  800. phb->ops = &iSeries_pci_ops;
  801. phb->io_base_virt = (void __iomem *)_IO_BASE;
  802. phb->io_resource.flags = IORESOURCE_IO;
  803. phb->io_resource.start = BASE_IO_MEMORY;
  804. phb->io_resource.end = END_IO_MEMORY;
  805. phb->io_resource.name = "iSeries PCI IO";
  806. phb->mem_resources[0].flags = IORESOURCE_MEM;
  807. phb->mem_resources[0].start = BASE_IO_MEMORY;
  808. phb->mem_resources[0].end = END_IO_MEMORY;
  809. phb->mem_resources[0].name = "Series PCI MEM";
  810. }
  811. of_node_put(root);
  812. pci_devs_phb_init();
  813. }