pci.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /*
  2. * Copyright (C) 2001 Allan Trautman, IBM Corporation
  3. *
  4. * iSeries specific routines for PCI.
  5. *
  6. * Based on code from pci.c and iSeries_pci.c 32bit
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/list.h>
  24. #include <linux/string.h>
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/ide.h>
  28. #include <linux/pci.h>
  29. #include <asm/io.h>
  30. #include <asm/irq.h>
  31. #include <asm/prom.h>
  32. #include <asm/machdep.h>
  33. #include <asm/pci-bridge.h>
  34. #include <asm/iommu.h>
  35. #include <asm/abs_addr.h>
  36. #include <asm/iseries/hv_call_xm.h>
  37. #include <asm/iseries/mf.h>
  38. #include <asm/ppc-pci.h>
  39. #include "irq.h"
  40. #include "pci.h"
  41. #include "call_pci.h"
  42. #include "iommu.h"
  43. extern unsigned long io_page_mask;
  44. /*
  45. * Forward declares of prototypes.
  46. */
  47. static struct device_node *find_Device_Node(int bus, int devfn);
  48. static void scan_PHB_slots(struct pci_controller *Phb);
  49. static void scan_EADS_bridge(HvBusNumber Bus, HvSubBusNumber SubBus, int IdSel);
  50. static int scan_bridge_slot(HvBusNumber Bus, struct HvCallPci_BridgeInfo *Info);
  51. LIST_HEAD(iSeries_Global_Device_List);
  52. static int DeviceCount;
  53. /* Counters and control flags. */
  54. static long Pci_Io_Read_Count;
  55. static long Pci_Io_Write_Count;
  56. #if 0
  57. static long Pci_Cfg_Read_Count;
  58. static long Pci_Cfg_Write_Count;
  59. #endif
  60. static long Pci_Error_Count;
  61. static int Pci_Retry_Max = 3; /* Only retry 3 times */
  62. static int Pci_Error_Flag = 1; /* Set Retry Error on. */
  63. static struct pci_ops iSeries_pci_ops;
  64. /*
  65. * Table defines
  66. * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
  67. */
  68. #define IOMM_TABLE_MAX_ENTRIES 1024
  69. #define IOMM_TABLE_ENTRY_SIZE 0x0000000000400000UL
  70. #define BASE_IO_MEMORY 0xE000000000000000UL
  71. static unsigned long max_io_memory = 0xE000000000000000UL;
  72. static long current_iomm_table_entry;
  73. /*
  74. * Lookup Tables.
  75. */
  76. static struct device_node **iomm_table;
  77. static u8 *iobar_table;
  78. /*
  79. * Static and Global variables
  80. */
  81. static char *pci_io_text = "iSeries PCI I/O";
  82. static DEFINE_SPINLOCK(iomm_table_lock);
  83. /*
  84. * iomm_table_initialize
  85. *
  86. * Allocates and initalizes the Address Translation Table and Bar
  87. * Tables to get them ready for use. Must be called before any
  88. * I/O space is handed out to the device BARs.
  89. */
  90. static void iomm_table_initialize(void)
  91. {
  92. spin_lock(&iomm_table_lock);
  93. iomm_table = kmalloc(sizeof(*iomm_table) * IOMM_TABLE_MAX_ENTRIES,
  94. GFP_KERNEL);
  95. iobar_table = kmalloc(sizeof(*iobar_table) * IOMM_TABLE_MAX_ENTRIES,
  96. GFP_KERNEL);
  97. spin_unlock(&iomm_table_lock);
  98. if ((iomm_table == NULL) || (iobar_table == NULL))
  99. panic("PCI: I/O tables allocation failed.\n");
  100. }
  101. /*
  102. * iomm_table_allocate_entry
  103. *
  104. * Adds pci_dev entry in address translation table
  105. *
  106. * - Allocates the number of entries required in table base on BAR
  107. * size.
  108. * - Allocates starting at BASE_IO_MEMORY and increases.
  109. * - The size is round up to be a multiple of entry size.
  110. * - CurrentIndex is incremented to keep track of the last entry.
  111. * - Builds the resource entry for allocated BARs.
  112. */
  113. static void iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
  114. {
  115. struct resource *bar_res = &dev->resource[bar_num];
  116. long bar_size = pci_resource_len(dev, bar_num);
  117. /*
  118. * No space to allocate, quick exit, skip Allocation.
  119. */
  120. if (bar_size == 0)
  121. return;
  122. /*
  123. * Set Resource values.
  124. */
  125. spin_lock(&iomm_table_lock);
  126. bar_res->name = pci_io_text;
  127. bar_res->start =
  128. IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
  129. bar_res->start += BASE_IO_MEMORY;
  130. bar_res->end = bar_res->start + bar_size - 1;
  131. /*
  132. * Allocate the number of table entries needed for BAR.
  133. */
  134. while (bar_size > 0 ) {
  135. iomm_table[current_iomm_table_entry] = dev->sysdata;
  136. iobar_table[current_iomm_table_entry] = bar_num;
  137. bar_size -= IOMM_TABLE_ENTRY_SIZE;
  138. ++current_iomm_table_entry;
  139. }
  140. max_io_memory = BASE_IO_MEMORY +
  141. (IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry);
  142. spin_unlock(&iomm_table_lock);
  143. }
  144. /*
  145. * allocate_device_bars
  146. *
  147. * - Allocates ALL pci_dev BAR's and updates the resources with the
  148. * BAR value. BARS with zero length will have the resources
  149. * The HvCallPci_getBarParms is used to get the size of the BAR
  150. * space. It calls iomm_table_allocate_entry to allocate
  151. * each entry.
  152. * - Loops through The Bar resources(0 - 5) including the ROM
  153. * is resource(6).
  154. */
  155. static void allocate_device_bars(struct pci_dev *dev)
  156. {
  157. struct resource *bar_res;
  158. int bar_num;
  159. for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num) {
  160. bar_res = &dev->resource[bar_num];
  161. iomm_table_allocate_entry(dev, bar_num);
  162. }
  163. }
  164. /*
  165. * Log error information to system console.
  166. * Filter out the device not there errors.
  167. * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
  168. * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
  169. * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
  170. */
  171. static void pci_Log_Error(char *Error_Text, int Bus, int SubBus,
  172. int AgentId, int HvRc)
  173. {
  174. if (HvRc == 0x0302)
  175. return;
  176. printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
  177. Error_Text, Bus, SubBus, AgentId, HvRc);
  178. }
  179. /*
  180. * build_device_node(u16 Bus, int SubBus, u8 DevFn)
  181. */
  182. static struct device_node *build_device_node(HvBusNumber Bus,
  183. HvSubBusNumber SubBus, int AgentId, int Function)
  184. {
  185. struct device_node *node;
  186. struct pci_dn *pdn;
  187. node = kmalloc(sizeof(struct device_node), GFP_KERNEL);
  188. if (node == NULL)
  189. return NULL;
  190. memset(node, 0, sizeof(struct device_node));
  191. pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
  192. if (pdn == NULL) {
  193. kfree(node);
  194. return NULL;
  195. }
  196. node->data = pdn;
  197. pdn->node = node;
  198. list_add_tail(&pdn->Device_List, &iSeries_Global_Device_List);
  199. pdn->busno = Bus;
  200. pdn->bussubno = SubBus;
  201. pdn->devfn = PCI_DEVFN(ISERIES_ENCODE_DEVICE(AgentId), Function);
  202. return node;
  203. }
  204. /*
  205. * unsigned long __init find_and_init_phbs(void)
  206. *
  207. * Description:
  208. * This function checks for all possible system PCI host bridges that connect
  209. * PCI buses. The system hypervisor is queried as to the guest partition
  210. * ownership status. A pci_controller is built for any bus which is partially
  211. * owned or fully owned by this guest partition.
  212. */
  213. unsigned long __init find_and_init_phbs(void)
  214. {
  215. struct pci_controller *phb;
  216. HvBusNumber bus;
  217. /* Check all possible buses. */
  218. for (bus = 0; bus < 256; bus++) {
  219. int ret = HvCallXm_testBus(bus);
  220. if (ret == 0) {
  221. printk("bus %d appears to exist\n", bus);
  222. phb = pcibios_alloc_controller(NULL);
  223. if (phb == NULL)
  224. return -ENOMEM;
  225. phb->pci_mem_offset = phb->local_number = bus;
  226. phb->first_busno = bus;
  227. phb->last_busno = bus;
  228. phb->ops = &iSeries_pci_ops;
  229. /* Find and connect the devices. */
  230. scan_PHB_slots(phb);
  231. }
  232. /*
  233. * Check for Unexpected Return code, a clue that something
  234. * has gone wrong.
  235. */
  236. else if (ret != 0x0301)
  237. printk(KERN_ERR "Unexpected Return on Probe(0x%04X): 0x%04X",
  238. bus, ret);
  239. }
  240. return 0;
  241. }
  242. /*
  243. * iSeries_pcibios_init
  244. *
  245. * Chance to initialize and structures or variable before PCI Bus walk.
  246. */
  247. void iSeries_pcibios_init(void)
  248. {
  249. iomm_table_initialize();
  250. find_and_init_phbs();
  251. io_page_mask = -1;
  252. }
  253. /*
  254. * iSeries_pci_final_fixup(void)
  255. */
  256. void __init iSeries_pci_final_fixup(void)
  257. {
  258. struct pci_dev *pdev = NULL;
  259. struct device_node *node;
  260. int DeviceCount = 0;
  261. /* Fix up at the device node and pci_dev relationship */
  262. mf_display_src(0xC9000100);
  263. printk("pcibios_final_fixup\n");
  264. for_each_pci_dev(pdev) {
  265. node = find_Device_Node(pdev->bus->number, pdev->devfn);
  266. printk("pci dev %p (%x.%x), node %p\n", pdev,
  267. pdev->bus->number, pdev->devfn, node);
  268. if (node != NULL) {
  269. ++DeviceCount;
  270. pdev->sysdata = (void *)node;
  271. PCI_DN(node)->pcidev = pdev;
  272. allocate_device_bars(pdev);
  273. iSeries_Device_Information(pdev, DeviceCount);
  274. iommu_devnode_init_iSeries(node);
  275. } else
  276. printk("PCI: Device Tree not found for 0x%016lX\n",
  277. (unsigned long)pdev);
  278. pdev->irq = PCI_DN(node)->Irq;
  279. }
  280. iSeries_activate_IRQs();
  281. mf_display_src(0xC9000200);
  282. }
  283. void pcibios_fixup_bus(struct pci_bus *PciBus)
  284. {
  285. }
  286. void pcibios_fixup_resources(struct pci_dev *pdev)
  287. {
  288. }
  289. /*
  290. * Loop through each node function to find usable EADs bridges.
  291. */
  292. static void scan_PHB_slots(struct pci_controller *Phb)
  293. {
  294. struct HvCallPci_DeviceInfo *DevInfo;
  295. HvBusNumber bus = Phb->local_number; /* System Bus */
  296. const HvSubBusNumber SubBus = 0; /* EADs is always 0. */
  297. int HvRc = 0;
  298. int IdSel;
  299. const int MaxAgents = 8;
  300. DevInfo = (struct HvCallPci_DeviceInfo*)
  301. kmalloc(sizeof(struct HvCallPci_DeviceInfo), GFP_KERNEL);
  302. if (DevInfo == NULL)
  303. return;
  304. /*
  305. * Probe for EADs Bridges
  306. */
  307. for (IdSel = 1; IdSel < MaxAgents; ++IdSel) {
  308. HvRc = HvCallPci_getDeviceInfo(bus, SubBus, IdSel,
  309. iseries_hv_addr(DevInfo),
  310. sizeof(struct HvCallPci_DeviceInfo));
  311. if (HvRc == 0) {
  312. if (DevInfo->deviceType == HvCallPci_NodeDevice)
  313. scan_EADS_bridge(bus, SubBus, IdSel);
  314. else
  315. printk("PCI: Invalid System Configuration(0x%02X)"
  316. " for bus 0x%02x id 0x%02x.\n",
  317. DevInfo->deviceType, bus, IdSel);
  318. }
  319. else
  320. pci_Log_Error("getDeviceInfo", bus, SubBus, IdSel, HvRc);
  321. }
  322. kfree(DevInfo);
  323. }
  324. static void scan_EADS_bridge(HvBusNumber bus, HvSubBusNumber SubBus,
  325. int IdSel)
  326. {
  327. struct HvCallPci_BridgeInfo *BridgeInfo;
  328. HvAgentId AgentId;
  329. int Function;
  330. int HvRc;
  331. BridgeInfo = (struct HvCallPci_BridgeInfo *)
  332. kmalloc(sizeof(struct HvCallPci_BridgeInfo), GFP_KERNEL);
  333. if (BridgeInfo == NULL)
  334. return;
  335. /* Note: hvSubBus and irq is always be 0 at this level! */
  336. for (Function = 0; Function < 8; ++Function) {
  337. AgentId = ISERIES_PCI_AGENTID(IdSel, Function);
  338. HvRc = HvCallXm_connectBusUnit(bus, SubBus, AgentId, 0);
  339. if (HvRc == 0) {
  340. printk("found device at bus %d idsel %d func %d (AgentId %x)\n",
  341. bus, IdSel, Function, AgentId);
  342. /* Connect EADs: 0x18.00.12 = 0x00 */
  343. HvRc = HvCallPci_getBusUnitInfo(bus, SubBus, AgentId,
  344. iseries_hv_addr(BridgeInfo),
  345. sizeof(struct HvCallPci_BridgeInfo));
  346. if (HvRc == 0) {
  347. printk("bridge info: type %x subbus %x maxAgents %x maxsubbus %x logslot %x\n",
  348. BridgeInfo->busUnitInfo.deviceType,
  349. BridgeInfo->subBusNumber,
  350. BridgeInfo->maxAgents,
  351. BridgeInfo->maxSubBusNumber,
  352. BridgeInfo->logicalSlotNumber);
  353. if (BridgeInfo->busUnitInfo.deviceType ==
  354. HvCallPci_BridgeDevice) {
  355. /* Scan_Bridge_Slot...: 0x18.00.12 */
  356. scan_bridge_slot(bus, BridgeInfo);
  357. } else
  358. printk("PCI: Invalid Bridge Configuration(0x%02X)",
  359. BridgeInfo->busUnitInfo.deviceType);
  360. }
  361. } else if (HvRc != 0x000B)
  362. pci_Log_Error("EADs Connect",
  363. bus, SubBus, AgentId, HvRc);
  364. }
  365. kfree(BridgeInfo);
  366. }
  367. /*
  368. * This assumes that the node slot is always on the primary bus!
  369. */
  370. static int scan_bridge_slot(HvBusNumber Bus,
  371. struct HvCallPci_BridgeInfo *BridgeInfo)
  372. {
  373. struct device_node *node;
  374. HvSubBusNumber SubBus = BridgeInfo->subBusNumber;
  375. u16 VendorId = 0;
  376. int HvRc = 0;
  377. u8 Irq = 0;
  378. int IdSel = ISERIES_GET_DEVICE_FROM_SUBBUS(SubBus);
  379. int Function = ISERIES_GET_FUNCTION_FROM_SUBBUS(SubBus);
  380. HvAgentId EADsIdSel = ISERIES_PCI_AGENTID(IdSel, Function);
  381. /* iSeries_allocate_IRQ.: 0x18.00.12(0xA3) */
  382. Irq = iSeries_allocate_IRQ(Bus, 0, EADsIdSel);
  383. /*
  384. * Connect all functions of any device found.
  385. */
  386. for (IdSel = 1; IdSel <= BridgeInfo->maxAgents; ++IdSel) {
  387. for (Function = 0; Function < 8; ++Function) {
  388. HvAgentId AgentId = ISERIES_PCI_AGENTID(IdSel, Function);
  389. HvRc = HvCallXm_connectBusUnit(Bus, SubBus,
  390. AgentId, Irq);
  391. if (HvRc != 0) {
  392. pci_Log_Error("Connect Bus Unit",
  393. Bus, SubBus, AgentId, HvRc);
  394. continue;
  395. }
  396. HvRc = HvCallPci_configLoad16(Bus, SubBus, AgentId,
  397. PCI_VENDOR_ID, &VendorId);
  398. if (HvRc != 0) {
  399. pci_Log_Error("Read Vendor",
  400. Bus, SubBus, AgentId, HvRc);
  401. continue;
  402. }
  403. printk("read vendor ID: %x\n", VendorId);
  404. /* FoundDevice: 0x18.28.10 = 0x12AE */
  405. HvRc = HvCallPci_configStore8(Bus, SubBus, AgentId,
  406. PCI_INTERRUPT_LINE, Irq);
  407. if (HvRc != 0)
  408. pci_Log_Error("PciCfgStore Irq Failed!",
  409. Bus, SubBus, AgentId, HvRc);
  410. ++DeviceCount;
  411. node = build_device_node(Bus, SubBus, EADsIdSel, Function);
  412. PCI_DN(node)->Irq = Irq;
  413. PCI_DN(node)->LogicalSlot = BridgeInfo->logicalSlotNumber;
  414. } /* for (Function = 0; Function < 8; ++Function) */
  415. } /* for (IdSel = 1; IdSel <= MaxAgents; ++IdSel) */
  416. return HvRc;
  417. }
  418. /*
  419. * I/0 Memory copy MUST use mmio commands on iSeries
  420. * To do; For performance, include the hv call directly
  421. */
  422. void iSeries_memset_io(volatile void __iomem *dest, char c, size_t Count)
  423. {
  424. u8 ByteValue = c;
  425. long NumberOfBytes = Count;
  426. while (NumberOfBytes > 0) {
  427. iSeries_Write_Byte(ByteValue, dest++);
  428. -- NumberOfBytes;
  429. }
  430. }
  431. EXPORT_SYMBOL(iSeries_memset_io);
  432. void iSeries_memcpy_toio(volatile void __iomem *dest, void *source, size_t count)
  433. {
  434. char *src = source;
  435. long NumberOfBytes = count;
  436. while (NumberOfBytes > 0) {
  437. iSeries_Write_Byte(*src++, dest++);
  438. -- NumberOfBytes;
  439. }
  440. }
  441. EXPORT_SYMBOL(iSeries_memcpy_toio);
  442. void iSeries_memcpy_fromio(void *dest, const volatile void __iomem *src, size_t count)
  443. {
  444. char *dst = dest;
  445. long NumberOfBytes = count;
  446. while (NumberOfBytes > 0) {
  447. *dst++ = iSeries_Read_Byte(src++);
  448. -- NumberOfBytes;
  449. }
  450. }
  451. EXPORT_SYMBOL(iSeries_memcpy_fromio);
  452. /*
  453. * Look down the chain to find the matching Device Device
  454. */
  455. static struct device_node *find_Device_Node(int bus, int devfn)
  456. {
  457. struct pci_dn *pdn;
  458. list_for_each_entry(pdn, &iSeries_Global_Device_List, Device_List) {
  459. if ((bus == pdn->busno) && (devfn == pdn->devfn))
  460. return pdn->node;
  461. }
  462. return NULL;
  463. }
  464. #if 0
  465. /*
  466. * Returns the device node for the passed pci_dev
  467. * Sanity Check Node PciDev to passed pci_dev
  468. * If none is found, returns a NULL which the client must handle.
  469. */
  470. static struct device_node *get_Device_Node(struct pci_dev *pdev)
  471. {
  472. struct device_node *node;
  473. node = pdev->sysdata;
  474. if (node == NULL || PCI_DN(node)->pcidev != pdev)
  475. node = find_Device_Node(pdev->bus->number, pdev->devfn);
  476. return node;
  477. }
  478. #endif
  479. /*
  480. * Config space read and write functions.
  481. * For now at least, we look for the device node for the bus and devfn
  482. * that we are asked to access. It may be possible to translate the devfn
  483. * to a subbus and deviceid more directly.
  484. */
  485. static u64 hv_cfg_read_func[4] = {
  486. HvCallPciConfigLoad8, HvCallPciConfigLoad16,
  487. HvCallPciConfigLoad32, HvCallPciConfigLoad32
  488. };
  489. static u64 hv_cfg_write_func[4] = {
  490. HvCallPciConfigStore8, HvCallPciConfigStore16,
  491. HvCallPciConfigStore32, HvCallPciConfigStore32
  492. };
  493. /*
  494. * Read PCI config space
  495. */
  496. static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
  497. int offset, int size, u32 *val)
  498. {
  499. struct device_node *node = find_Device_Node(bus->number, devfn);
  500. u64 fn;
  501. struct HvCallPci_LoadReturn ret;
  502. if (node == NULL)
  503. return PCIBIOS_DEVICE_NOT_FOUND;
  504. if (offset > 255) {
  505. *val = ~0;
  506. return PCIBIOS_BAD_REGISTER_NUMBER;
  507. }
  508. fn = hv_cfg_read_func[(size - 1) & 3];
  509. HvCall3Ret16(fn, &ret, iseries_ds_addr(node), offset, 0);
  510. if (ret.rc != 0) {
  511. *val = ~0;
  512. return PCIBIOS_DEVICE_NOT_FOUND; /* or something */
  513. }
  514. *val = ret.value;
  515. return 0;
  516. }
  517. /*
  518. * Write PCI config space
  519. */
  520. static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
  521. int offset, int size, u32 val)
  522. {
  523. struct device_node *node = find_Device_Node(bus->number, devfn);
  524. u64 fn;
  525. u64 ret;
  526. if (node == NULL)
  527. return PCIBIOS_DEVICE_NOT_FOUND;
  528. if (offset > 255)
  529. return PCIBIOS_BAD_REGISTER_NUMBER;
  530. fn = hv_cfg_write_func[(size - 1) & 3];
  531. ret = HvCall4(fn, iseries_ds_addr(node), offset, val, 0);
  532. if (ret != 0)
  533. return PCIBIOS_DEVICE_NOT_FOUND;
  534. return 0;
  535. }
  536. static struct pci_ops iSeries_pci_ops = {
  537. .read = iSeries_pci_read_config,
  538. .write = iSeries_pci_write_config
  539. };
  540. /*
  541. * Check Return Code
  542. * -> On Failure, print and log information.
  543. * Increment Retry Count, if exceeds max, panic partition.
  544. *
  545. * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
  546. * PCI: Device 23.90 ReadL Retry( 1)
  547. * PCI: Device 23.90 ReadL Retry Successful(1)
  548. */
  549. static int CheckReturnCode(char *TextHdr, struct device_node *DevNode,
  550. int *retry, u64 ret)
  551. {
  552. if (ret != 0) {
  553. struct pci_dn *pdn = PCI_DN(DevNode);
  554. ++Pci_Error_Count;
  555. (*retry)++;
  556. printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
  557. TextHdr, pdn->busno, pdn->devfn,
  558. *retry, (int)ret);
  559. /*
  560. * Bump the retry and check for retry count exceeded.
  561. * If, Exceeded, panic the system.
  562. */
  563. if (((*retry) > Pci_Retry_Max) &&
  564. (Pci_Error_Flag > 0)) {
  565. mf_display_src(0xB6000103);
  566. panic_timeout = 0;
  567. panic("PCI: Hardware I/O Error, SRC B6000103, "
  568. "Automatic Reboot Disabled.\n");
  569. }
  570. return -1; /* Retry Try */
  571. }
  572. return 0;
  573. }
  574. /*
  575. * Translate the I/O Address into a device node, bar, and bar offset.
  576. * Note: Make sure the passed variable end up on the stack to avoid
  577. * the exposure of being device global.
  578. */
  579. static inline struct device_node *xlate_iomm_address(
  580. const volatile void __iomem *IoAddress,
  581. u64 *dsaptr, u64 *BarOffsetPtr)
  582. {
  583. unsigned long OrigIoAddr;
  584. unsigned long BaseIoAddr;
  585. unsigned long TableIndex;
  586. struct device_node *DevNode;
  587. OrigIoAddr = (unsigned long __force)IoAddress;
  588. if ((OrigIoAddr < BASE_IO_MEMORY) || (OrigIoAddr >= max_io_memory))
  589. return NULL;
  590. BaseIoAddr = OrigIoAddr - BASE_IO_MEMORY;
  591. TableIndex = BaseIoAddr / IOMM_TABLE_ENTRY_SIZE;
  592. DevNode = iomm_table[TableIndex];
  593. if (DevNode != NULL) {
  594. int barnum = iobar_table[TableIndex];
  595. *dsaptr = iseries_ds_addr(DevNode) | (barnum << 24);
  596. *BarOffsetPtr = BaseIoAddr % IOMM_TABLE_ENTRY_SIZE;
  597. } else
  598. panic("PCI: Invalid PCI IoAddress detected!\n");
  599. return DevNode;
  600. }
  601. /*
  602. * Read MM I/O Instructions for the iSeries
  603. * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
  604. * else, data is returned in big Endian format.
  605. *
  606. * iSeries_Read_Byte = Read Byte ( 8 bit)
  607. * iSeries_Read_Word = Read Word (16 bit)
  608. * iSeries_Read_Long = Read Long (32 bit)
  609. */
  610. u8 iSeries_Read_Byte(const volatile void __iomem *IoAddress)
  611. {
  612. u64 BarOffset;
  613. u64 dsa;
  614. int retry = 0;
  615. struct HvCallPci_LoadReturn ret;
  616. struct device_node *DevNode =
  617. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  618. if (DevNode == NULL) {
  619. static unsigned long last_jiffies;
  620. static int num_printed;
  621. if ((jiffies - last_jiffies) > 60 * HZ) {
  622. last_jiffies = jiffies;
  623. num_printed = 0;
  624. }
  625. if (num_printed++ < 10)
  626. printk(KERN_ERR "iSeries_Read_Byte: invalid access at IO address %p\n", IoAddress);
  627. return 0xff;
  628. }
  629. do {
  630. ++Pci_Io_Read_Count;
  631. HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, BarOffset, 0);
  632. } while (CheckReturnCode("RDB", DevNode, &retry, ret.rc) != 0);
  633. return (u8)ret.value;
  634. }
  635. EXPORT_SYMBOL(iSeries_Read_Byte);
  636. u16 iSeries_Read_Word(const volatile void __iomem *IoAddress)
  637. {
  638. u64 BarOffset;
  639. u64 dsa;
  640. int retry = 0;
  641. struct HvCallPci_LoadReturn ret;
  642. struct device_node *DevNode =
  643. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  644. if (DevNode == NULL) {
  645. static unsigned long last_jiffies;
  646. static int num_printed;
  647. if ((jiffies - last_jiffies) > 60 * HZ) {
  648. last_jiffies = jiffies;
  649. num_printed = 0;
  650. }
  651. if (num_printed++ < 10)
  652. printk(KERN_ERR "iSeries_Read_Word: invalid access at IO address %p\n", IoAddress);
  653. return 0xffff;
  654. }
  655. do {
  656. ++Pci_Io_Read_Count;
  657. HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
  658. BarOffset, 0);
  659. } while (CheckReturnCode("RDW", DevNode, &retry, ret.rc) != 0);
  660. return swab16((u16)ret.value);
  661. }
  662. EXPORT_SYMBOL(iSeries_Read_Word);
  663. u32 iSeries_Read_Long(const volatile void __iomem *IoAddress)
  664. {
  665. u64 BarOffset;
  666. u64 dsa;
  667. int retry = 0;
  668. struct HvCallPci_LoadReturn ret;
  669. struct device_node *DevNode =
  670. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  671. if (DevNode == NULL) {
  672. static unsigned long last_jiffies;
  673. static int num_printed;
  674. if ((jiffies - last_jiffies) > 60 * HZ) {
  675. last_jiffies = jiffies;
  676. num_printed = 0;
  677. }
  678. if (num_printed++ < 10)
  679. printk(KERN_ERR "iSeries_Read_Long: invalid access at IO address %p\n", IoAddress);
  680. return 0xffffffff;
  681. }
  682. do {
  683. ++Pci_Io_Read_Count;
  684. HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
  685. BarOffset, 0);
  686. } while (CheckReturnCode("RDL", DevNode, &retry, ret.rc) != 0);
  687. return swab32((u32)ret.value);
  688. }
  689. EXPORT_SYMBOL(iSeries_Read_Long);
  690. /*
  691. * Write MM I/O Instructions for the iSeries
  692. *
  693. * iSeries_Write_Byte = Write Byte (8 bit)
  694. * iSeries_Write_Word = Write Word(16 bit)
  695. * iSeries_Write_Long = Write Long(32 bit)
  696. */
  697. void iSeries_Write_Byte(u8 data, volatile void __iomem *IoAddress)
  698. {
  699. u64 BarOffset;
  700. u64 dsa;
  701. int retry = 0;
  702. u64 rc;
  703. struct device_node *DevNode =
  704. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  705. if (DevNode == NULL) {
  706. static unsigned long last_jiffies;
  707. static int num_printed;
  708. if ((jiffies - last_jiffies) > 60 * HZ) {
  709. last_jiffies = jiffies;
  710. num_printed = 0;
  711. }
  712. if (num_printed++ < 10)
  713. printk(KERN_ERR "iSeries_Write_Byte: invalid access at IO address %p\n", IoAddress);
  714. return;
  715. }
  716. do {
  717. ++Pci_Io_Write_Count;
  718. rc = HvCall4(HvCallPciBarStore8, dsa, BarOffset, data, 0);
  719. } while (CheckReturnCode("WWB", DevNode, &retry, rc) != 0);
  720. }
  721. EXPORT_SYMBOL(iSeries_Write_Byte);
  722. void iSeries_Write_Word(u16 data, volatile void __iomem *IoAddress)
  723. {
  724. u64 BarOffset;
  725. u64 dsa;
  726. int retry = 0;
  727. u64 rc;
  728. struct device_node *DevNode =
  729. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  730. if (DevNode == NULL) {
  731. static unsigned long last_jiffies;
  732. static int num_printed;
  733. if ((jiffies - last_jiffies) > 60 * HZ) {
  734. last_jiffies = jiffies;
  735. num_printed = 0;
  736. }
  737. if (num_printed++ < 10)
  738. printk(KERN_ERR "iSeries_Write_Word: invalid access at IO address %p\n", IoAddress);
  739. return;
  740. }
  741. do {
  742. ++Pci_Io_Write_Count;
  743. rc = HvCall4(HvCallPciBarStore16, dsa, BarOffset, swab16(data), 0);
  744. } while (CheckReturnCode("WWW", DevNode, &retry, rc) != 0);
  745. }
  746. EXPORT_SYMBOL(iSeries_Write_Word);
  747. void iSeries_Write_Long(u32 data, volatile void __iomem *IoAddress)
  748. {
  749. u64 BarOffset;
  750. u64 dsa;
  751. int retry = 0;
  752. u64 rc;
  753. struct device_node *DevNode =
  754. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  755. if (DevNode == NULL) {
  756. static unsigned long last_jiffies;
  757. static int num_printed;
  758. if ((jiffies - last_jiffies) > 60 * HZ) {
  759. last_jiffies = jiffies;
  760. num_printed = 0;
  761. }
  762. if (num_printed++ < 10)
  763. printk(KERN_ERR "iSeries_Write_Long: invalid access at IO address %p\n", IoAddress);
  764. return;
  765. }
  766. do {
  767. ++Pci_Io_Write_Count;
  768. rc = HvCall4(HvCallPciBarStore32, dsa, BarOffset, swab32(data), 0);
  769. } while (CheckReturnCode("WWL", DevNode, &retry, rc) != 0);
  770. }
  771. EXPORT_SYMBOL(iSeries_Write_Long);