pci.c 22 KB

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