pci.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. * Copyright 2011 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/pci.h>
  16. #include <linux/delay.h>
  17. #include <linux/string.h>
  18. #include <linux/init.h>
  19. #include <linux/capability.h>
  20. #include <linux/sched.h>
  21. #include <linux/errno.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/irq.h>
  24. #include <linux/io.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/export.h>
  27. #include <asm/processor.h>
  28. #include <asm/sections.h>
  29. #include <asm/byteorder.h>
  30. #include <asm/hv_driver.h>
  31. #include <hv/drv_pcie_rc_intf.h>
  32. /*
  33. * Initialization flow and process
  34. * -------------------------------
  35. *
  36. * This files contains the routines to search for PCI buses,
  37. * enumerate the buses, and configure any attached devices.
  38. *
  39. * There are two entry points here:
  40. * 1) tile_pci_init
  41. * This sets up the pci_controller structs, and opens the
  42. * FDs to the hypervisor. This is called from setup_arch() early
  43. * in the boot process.
  44. * 2) pcibios_init
  45. * This probes the PCI bus(es) for any attached hardware. It's
  46. * called by subsys_initcall. All of the real work is done by the
  47. * generic Linux PCI layer.
  48. *
  49. */
  50. /*
  51. * This flag tells if the platform is TILEmpower that needs
  52. * special configuration for the PLX switch chip.
  53. */
  54. int __write_once tile_plx_gen1;
  55. static struct pci_controller controllers[TILE_NUM_PCIE];
  56. static int num_controllers;
  57. static int pci_scan_flags[TILE_NUM_PCIE];
  58. static struct pci_ops tile_cfg_ops;
  59. /*
  60. * We don't need to worry about the alignment of resources.
  61. */
  62. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  63. resource_size_t size, resource_size_t align)
  64. {
  65. return res->start;
  66. }
  67. EXPORT_SYMBOL(pcibios_align_resource);
  68. /*
  69. * Open a FD to the hypervisor PCI device.
  70. *
  71. * controller_id is the controller number, config type is 0 or 1 for
  72. * config0 or config1 operations.
  73. */
  74. static int __devinit tile_pcie_open(int controller_id, int config_type)
  75. {
  76. char filename[32];
  77. int fd;
  78. sprintf(filename, "pcie/%d/config%d", controller_id, config_type);
  79. fd = hv_dev_open((HV_VirtAddr)filename, 0);
  80. return fd;
  81. }
  82. /*
  83. * Get the IRQ numbers from the HV and set up the handlers for them.
  84. */
  85. static int __devinit tile_init_irqs(int controller_id,
  86. struct pci_controller *controller)
  87. {
  88. char filename[32];
  89. int fd;
  90. int ret;
  91. int x;
  92. struct pcie_rc_config rc_config;
  93. sprintf(filename, "pcie/%d/ctl", controller_id);
  94. fd = hv_dev_open((HV_VirtAddr)filename, 0);
  95. if (fd < 0) {
  96. pr_err("PCI: hv_dev_open(%s) failed\n", filename);
  97. return -1;
  98. }
  99. ret = hv_dev_pread(fd, 0, (HV_VirtAddr)(&rc_config),
  100. sizeof(rc_config), PCIE_RC_CONFIG_MASK_OFF);
  101. hv_dev_close(fd);
  102. if (ret != sizeof(rc_config)) {
  103. pr_err("PCI: wanted %zd bytes, got %d\n",
  104. sizeof(rc_config), ret);
  105. return -1;
  106. }
  107. /* Record irq_base so that we can map INTx to IRQ # later. */
  108. controller->irq_base = rc_config.intr;
  109. for (x = 0; x < 4; x++)
  110. tile_irq_activate(rc_config.intr + x,
  111. TILE_IRQ_HW_CLEAR);
  112. if (rc_config.plx_gen1)
  113. controller->plx_gen1 = 1;
  114. return 0;
  115. }
  116. /*
  117. * First initialization entry point, called from setup_arch().
  118. *
  119. * Find valid controllers and fill in pci_controller structs for each
  120. * of them.
  121. *
  122. * Returns the number of controllers discovered.
  123. */
  124. int __init tile_pci_init(void)
  125. {
  126. int i;
  127. pr_info("PCI: Searching for controllers...\n");
  128. /* Re-init number of PCIe controllers to support hot-plug feature. */
  129. num_controllers = 0;
  130. /* Do any configuration we need before using the PCIe */
  131. for (i = 0; i < TILE_NUM_PCIE; i++) {
  132. /*
  133. * To see whether we need a real config op based on
  134. * the results of pcibios_init(), to support PCIe hot-plug.
  135. */
  136. if (pci_scan_flags[i] == 0) {
  137. int hv_cfg_fd0 = -1;
  138. int hv_cfg_fd1 = -1;
  139. int hv_mem_fd = -1;
  140. char name[32];
  141. struct pci_controller *controller;
  142. /*
  143. * Open the fd to the HV. If it fails then this
  144. * device doesn't exist.
  145. */
  146. hv_cfg_fd0 = tile_pcie_open(i, 0);
  147. if (hv_cfg_fd0 < 0)
  148. continue;
  149. hv_cfg_fd1 = tile_pcie_open(i, 1);
  150. if (hv_cfg_fd1 < 0) {
  151. pr_err("PCI: Couldn't open config fd to HV "
  152. "for controller %d\n", i);
  153. goto err_cont;
  154. }
  155. sprintf(name, "pcie/%d/mem", i);
  156. hv_mem_fd = hv_dev_open((HV_VirtAddr)name, 0);
  157. if (hv_mem_fd < 0) {
  158. pr_err("PCI: Could not open mem fd to HV!\n");
  159. goto err_cont;
  160. }
  161. pr_info("PCI: Found PCI controller #%d\n", i);
  162. controller = &controllers[i];
  163. controller->index = i;
  164. controller->hv_cfg_fd[0] = hv_cfg_fd0;
  165. controller->hv_cfg_fd[1] = hv_cfg_fd1;
  166. controller->hv_mem_fd = hv_mem_fd;
  167. controller->first_busno = 0;
  168. controller->last_busno = 0xff;
  169. controller->ops = &tile_cfg_ops;
  170. num_controllers++;
  171. continue;
  172. err_cont:
  173. if (hv_cfg_fd0 >= 0)
  174. hv_dev_close(hv_cfg_fd0);
  175. if (hv_cfg_fd1 >= 0)
  176. hv_dev_close(hv_cfg_fd1);
  177. if (hv_mem_fd >= 0)
  178. hv_dev_close(hv_mem_fd);
  179. continue;
  180. }
  181. }
  182. /*
  183. * Before using the PCIe, see if we need to do any platform-specific
  184. * configuration, such as the PLX switch Gen 1 issue on TILEmpower.
  185. */
  186. for (i = 0; i < num_controllers; i++) {
  187. struct pci_controller *controller = &controllers[i];
  188. if (controller->plx_gen1)
  189. tile_plx_gen1 = 1;
  190. }
  191. return num_controllers;
  192. }
  193. /*
  194. * (pin - 1) converts from the PCI standard's [1:4] convention to
  195. * a normal [0:3] range.
  196. */
  197. static int tile_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  198. {
  199. struct pci_controller *controller =
  200. (struct pci_controller *)dev->sysdata;
  201. return (pin - 1) + controller->irq_base;
  202. }
  203. static void __devinit fixup_read_and_payload_sizes(void)
  204. {
  205. struct pci_dev *dev = NULL;
  206. int smallest_max_payload = 0x1; /* Tile maxes out at 256 bytes. */
  207. int max_read_size = 0x2; /* Limit to 512 byte reads. */
  208. u16 new_values;
  209. /* Scan for the smallest maximum payload size. */
  210. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  211. int pcie_caps_offset;
  212. u32 devcap;
  213. int max_payload;
  214. pcie_caps_offset = pci_find_capability(dev, PCI_CAP_ID_EXP);
  215. if (pcie_caps_offset == 0)
  216. continue;
  217. pci_read_config_dword(dev, pcie_caps_offset + PCI_EXP_DEVCAP,
  218. &devcap);
  219. max_payload = devcap & PCI_EXP_DEVCAP_PAYLOAD;
  220. if (max_payload < smallest_max_payload)
  221. smallest_max_payload = max_payload;
  222. }
  223. /* Now, set the max_payload_size for all devices to that value. */
  224. new_values = (max_read_size << 12) | (smallest_max_payload << 5);
  225. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  226. int pcie_caps_offset;
  227. u16 devctl;
  228. pcie_caps_offset = pci_find_capability(dev, PCI_CAP_ID_EXP);
  229. if (pcie_caps_offset == 0)
  230. continue;
  231. pci_read_config_word(dev, pcie_caps_offset + PCI_EXP_DEVCTL,
  232. &devctl);
  233. devctl &= ~(PCI_EXP_DEVCTL_PAYLOAD | PCI_EXP_DEVCTL_READRQ);
  234. devctl |= new_values;
  235. pci_write_config_word(dev, pcie_caps_offset + PCI_EXP_DEVCTL,
  236. devctl);
  237. }
  238. }
  239. /*
  240. * Second PCI initialization entry point, called by subsys_initcall.
  241. *
  242. * The controllers have been set up by the time we get here, by a call to
  243. * tile_pci_init.
  244. */
  245. int __init pcibios_init(void)
  246. {
  247. int i;
  248. pr_info("PCI: Probing PCI hardware\n");
  249. /*
  250. * Delay a bit in case devices aren't ready. Some devices are
  251. * known to require at least 20ms here, but we use a more
  252. * conservative value.
  253. */
  254. mdelay(250);
  255. /* Scan all of the recorded PCI controllers. */
  256. for (i = 0; i < TILE_NUM_PCIE; i++) {
  257. /*
  258. * Do real pcibios init ops if the controller is initialized
  259. * by tile_pci_init() successfully and not initialized by
  260. * pcibios_init() yet to support PCIe hot-plug.
  261. */
  262. if (pci_scan_flags[i] == 0 && controllers[i].ops != NULL) {
  263. struct pci_controller *controller = &controllers[i];
  264. struct pci_bus *bus;
  265. LIST_HEAD(resources);
  266. if (tile_init_irqs(i, controller)) {
  267. pr_err("PCI: Could not initialize IRQs\n");
  268. continue;
  269. }
  270. pr_info("PCI: initializing controller #%d\n", i);
  271. /*
  272. * This comes from the generic Linux PCI driver.
  273. *
  274. * It reads the PCI tree for this bus into the Linux
  275. * data structures.
  276. *
  277. * This is inlined in linux/pci.h and calls into
  278. * pci_scan_bus_parented() in probe.c.
  279. */
  280. pci_add_resource(&resources, &ioport_resource);
  281. pci_add_resource(&resources, &iomem_resource);
  282. bus = pci_scan_root_bus(NULL, 0, controller->ops, controller, &resources);
  283. controller->root_bus = bus;
  284. controller->last_busno = bus->busn_res.end;
  285. }
  286. }
  287. /* Do machine dependent PCI interrupt routing */
  288. pci_fixup_irqs(pci_common_swizzle, tile_map_irq);
  289. /*
  290. * This comes from the generic Linux PCI driver.
  291. *
  292. * It allocates all of the resources (I/O memory, etc)
  293. * associated with the devices read in above.
  294. */
  295. pci_assign_unassigned_resources();
  296. /* Configure the max_read_size and max_payload_size values. */
  297. fixup_read_and_payload_sizes();
  298. /* Record the I/O resources in the PCI controller structure. */
  299. for (i = 0; i < TILE_NUM_PCIE; i++) {
  300. /*
  301. * Do real pcibios init ops if the controller is initialized
  302. * by tile_pci_init() successfully and not initialized by
  303. * pcibios_init() yet to support PCIe hot-plug.
  304. */
  305. if (pci_scan_flags[i] == 0 && controllers[i].ops != NULL) {
  306. struct pci_bus *root_bus = controllers[i].root_bus;
  307. struct pci_bus *next_bus;
  308. struct pci_dev *dev;
  309. list_for_each_entry(dev, &root_bus->devices, bus_list) {
  310. /*
  311. * Find the PCI host controller, ie. the 1st
  312. * bridge.
  313. */
  314. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
  315. (PCI_SLOT(dev->devfn) == 0)) {
  316. next_bus = dev->busn_res.end;
  317. controllers[i].mem_resources[0] =
  318. *next_bus->resource[0];
  319. controllers[i].mem_resources[1] =
  320. *next_bus->resource[1];
  321. controllers[i].mem_resources[2] =
  322. *next_bus->resource[2];
  323. /* Setup flags. */
  324. pci_scan_flags[i] = 1;
  325. break;
  326. }
  327. }
  328. }
  329. }
  330. return 0;
  331. }
  332. subsys_initcall(pcibios_init);
  333. /*
  334. * No bus fixups needed.
  335. */
  336. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  337. {
  338. /* Nothing needs to be done. */
  339. }
  340. void pcibios_set_master(struct pci_dev *dev)
  341. {
  342. /* No special bus mastering setup handling. */
  343. }
  344. /*
  345. * This can be called from the generic PCI layer, but doesn't need to
  346. * do anything.
  347. */
  348. char __devinit *pcibios_setup(char *str)
  349. {
  350. /* Nothing needs to be done. */
  351. return str;
  352. }
  353. /*
  354. * This is called from the generic Linux layer.
  355. */
  356. void __devinit pcibios_update_irq(struct pci_dev *dev, int irq)
  357. {
  358. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  359. }
  360. /*
  361. * Enable memory and/or address decoding, as appropriate, for the
  362. * device described by the 'dev' struct.
  363. *
  364. * This is called from the generic PCI layer, and can be called
  365. * for bridges or endpoints.
  366. */
  367. int pcibios_enable_device(struct pci_dev *dev, int mask)
  368. {
  369. u16 cmd, old_cmd;
  370. u8 header_type;
  371. int i;
  372. struct resource *r;
  373. pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
  374. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  375. old_cmd = cmd;
  376. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
  377. /*
  378. * For bridges, we enable both memory and I/O decoding
  379. * in call cases.
  380. */
  381. cmd |= PCI_COMMAND_IO;
  382. cmd |= PCI_COMMAND_MEMORY;
  383. } else {
  384. /*
  385. * For endpoints, we enable memory and/or I/O decoding
  386. * only if they have a memory resource of that type.
  387. */
  388. for (i = 0; i < 6; i++) {
  389. r = &dev->resource[i];
  390. if (r->flags & IORESOURCE_UNSET) {
  391. pr_err("PCI: Device %s not available "
  392. "because of resource collisions\n",
  393. pci_name(dev));
  394. return -EINVAL;
  395. }
  396. if (r->flags & IORESOURCE_IO)
  397. cmd |= PCI_COMMAND_IO;
  398. if (r->flags & IORESOURCE_MEM)
  399. cmd |= PCI_COMMAND_MEMORY;
  400. }
  401. }
  402. /*
  403. * We only write the command if it changed.
  404. */
  405. if (cmd != old_cmd)
  406. pci_write_config_word(dev, PCI_COMMAND, cmd);
  407. return 0;
  408. }
  409. /****************************************************************
  410. *
  411. * Tile PCI config space read/write routines
  412. *
  413. ****************************************************************/
  414. /*
  415. * These are the normal read and write ops
  416. * These are expanded with macros from pci_bus_read_config_byte() etc.
  417. *
  418. * devfn is the combined PCI slot & function.
  419. *
  420. * offset is in bytes, from the start of config space for the
  421. * specified bus & slot.
  422. */
  423. static int __devinit tile_cfg_read(struct pci_bus *bus,
  424. unsigned int devfn,
  425. int offset,
  426. int size,
  427. u32 *val)
  428. {
  429. struct pci_controller *controller = bus->sysdata;
  430. int busnum = bus->number & 0xff;
  431. int slot = (devfn >> 3) & 0x1f;
  432. int function = devfn & 0x7;
  433. u32 addr;
  434. int config_mode = 1;
  435. /*
  436. * There is no bridge between the Tile and bus 0, so we
  437. * use config0 to talk to bus 0.
  438. *
  439. * If we're talking to a bus other than zero then we
  440. * must have found a bridge.
  441. */
  442. if (busnum == 0) {
  443. /*
  444. * We fake an empty slot for (busnum == 0) && (slot > 0),
  445. * since there is only one slot on bus 0.
  446. */
  447. if (slot) {
  448. *val = 0xFFFFFFFF;
  449. return 0;
  450. }
  451. config_mode = 0;
  452. }
  453. addr = busnum << 20; /* Bus in 27:20 */
  454. addr |= slot << 15; /* Slot (device) in 19:15 */
  455. addr |= function << 12; /* Function is in 14:12 */
  456. addr |= (offset & 0xFFF); /* byte address in 0:11 */
  457. return hv_dev_pread(controller->hv_cfg_fd[config_mode], 0,
  458. (HV_VirtAddr)(val), size, addr);
  459. }
  460. /*
  461. * See tile_cfg_read() for relevant comments.
  462. * Note that "val" is the value to write, not a pointer to that value.
  463. */
  464. static int __devinit tile_cfg_write(struct pci_bus *bus,
  465. unsigned int devfn,
  466. int offset,
  467. int size,
  468. u32 val)
  469. {
  470. struct pci_controller *controller = bus->sysdata;
  471. int busnum = bus->number & 0xff;
  472. int slot = (devfn >> 3) & 0x1f;
  473. int function = devfn & 0x7;
  474. u32 addr;
  475. int config_mode = 1;
  476. HV_VirtAddr valp = (HV_VirtAddr)&val;
  477. /*
  478. * For bus 0 slot 0 we use config 0 accesses.
  479. */
  480. if (busnum == 0) {
  481. /*
  482. * We fake an empty slot for (busnum == 0) && (slot > 0),
  483. * since there is only one slot on bus 0.
  484. */
  485. if (slot)
  486. return 0;
  487. config_mode = 0;
  488. }
  489. addr = busnum << 20; /* Bus in 27:20 */
  490. addr |= slot << 15; /* Slot (device) in 19:15 */
  491. addr |= function << 12; /* Function is in 14:12 */
  492. addr |= (offset & 0xFFF); /* byte address in 0:11 */
  493. #ifdef __BIG_ENDIAN
  494. /* Point to the correct part of the 32-bit "val". */
  495. valp += 4 - size;
  496. #endif
  497. return hv_dev_pwrite(controller->hv_cfg_fd[config_mode], 0,
  498. valp, size, addr);
  499. }
  500. static struct pci_ops tile_cfg_ops = {
  501. .read = tile_cfg_read,
  502. .write = tile_cfg_write,
  503. };
  504. /*
  505. * In the following, each PCI controller's mem_resources[1]
  506. * represents its (non-prefetchable) PCI memory resource.
  507. * mem_resources[0] and mem_resources[2] refer to its PCI I/O and
  508. * prefetchable PCI memory resources, respectively.
  509. * For more details, see pci_setup_bridge() in setup-bus.c.
  510. * By comparing the target PCI memory address against the
  511. * end address of controller 0, we can determine the controller
  512. * that should accept the PCI memory access.
  513. */
  514. #define TILE_READ(size, type) \
  515. type _tile_read##size(unsigned long addr) \
  516. { \
  517. type val; \
  518. int idx = 0; \
  519. if (addr > controllers[0].mem_resources[1].end && \
  520. addr > controllers[0].mem_resources[2].end) \
  521. idx = 1; \
  522. if (hv_dev_pread(controllers[idx].hv_mem_fd, 0, \
  523. (HV_VirtAddr)(&val), sizeof(type), addr)) \
  524. pr_err("PCI: read %zd bytes at 0x%lX failed\n", \
  525. sizeof(type), addr); \
  526. return val; \
  527. } \
  528. EXPORT_SYMBOL(_tile_read##size)
  529. TILE_READ(b, u8);
  530. TILE_READ(w, u16);
  531. TILE_READ(l, u32);
  532. TILE_READ(q, u64);
  533. #define TILE_WRITE(size, type) \
  534. void _tile_write##size(type val, unsigned long addr) \
  535. { \
  536. int idx = 0; \
  537. if (addr > controllers[0].mem_resources[1].end && \
  538. addr > controllers[0].mem_resources[2].end) \
  539. idx = 1; \
  540. if (hv_dev_pwrite(controllers[idx].hv_mem_fd, 0, \
  541. (HV_VirtAddr)(&val), sizeof(type), addr)) \
  542. pr_err("PCI: write %zd bytes at 0x%lX failed\n", \
  543. sizeof(type), addr); \
  544. } \
  545. EXPORT_SYMBOL(_tile_write##size)
  546. TILE_WRITE(b, u8);
  547. TILE_WRITE(w, u16);
  548. TILE_WRITE(l, u32);
  549. TILE_WRITE(q, u64);