drivers.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * drivers.c
  4. *
  5. * Copyright (c) 1999 The Puffin Group
  6. * Copyright (c) 2001 Matthew Wilcox for Hewlett Packard
  7. * Copyright (c) 2001-2023 Helge Deller <deller@gmx.de>
  8. * Copyright (c) 2001,2002 Ryan Bradetich
  9. * Copyright (c) 2004-2005 Thibaut VARENE <varenet@parisc-linux.org>
  10. *
  11. * The file handles registering devices and drivers, then matching them.
  12. * It's the closest we get to a dating agency.
  13. *
  14. * If you're thinking about modifying this file, here are some gotchas to
  15. * bear in mind:
  16. * - 715/Mirage device paths have a dummy device between Lasi and its children
  17. * - The EISA adapter may show up as a sibling or child of Wax
  18. * - Dino has an optionally functional serial port. If firmware enables it,
  19. * it shows up as a child of Dino. If firmware disables it, the buswalk
  20. * finds it and it shows up as a child of Cujo
  21. * - Dino has both parisc and pci devices as children
  22. * - parisc devices are discovered in a random order, including children
  23. * before parents in some cases.
  24. */
  25. #include <linux/slab.h>
  26. #include <linux/types.h>
  27. #include <linux/kernel.h>
  28. #include <linux/pci.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/string.h>
  31. #include <linux/export.h>
  32. #include <linux/dma-map-ops.h>
  33. #include <asm/hardware.h>
  34. #include <asm/io.h>
  35. #include <asm/pdc.h>
  36. #include <asm/parisc-device.h>
  37. #include <asm/ropes.h>
  38. /* See comments in include/asm-parisc/pci.h */
  39. const struct dma_map_ops *hppa_dma_ops __ro_after_init;
  40. EXPORT_SYMBOL(hppa_dma_ops);
  41. static struct device root = {
  42. .init_name = "parisc",
  43. };
  44. static inline int check_dev(struct device *dev)
  45. {
  46. if (dev->bus == &parisc_bus_type) {
  47. struct parisc_device *pdev;
  48. pdev = to_parisc_device(dev);
  49. return pdev->id.hw_type != HPHW_FAULTY;
  50. }
  51. return 1;
  52. }
  53. static struct device *
  54. parse_tree_node(struct device *parent, int index, struct hardware_path *modpath);
  55. struct recurse_struct {
  56. void * obj;
  57. int (*fn)(struct device *, void *);
  58. };
  59. static int descend_children(struct device * dev, void * data)
  60. {
  61. struct recurse_struct * recurse_data = (struct recurse_struct *)data;
  62. if (recurse_data->fn(dev, recurse_data->obj))
  63. return 1;
  64. else
  65. return device_for_each_child(dev, recurse_data, descend_children);
  66. }
  67. /**
  68. * for_each_padev - Iterate over all devices in the tree
  69. * @fn: Function to call for each device.
  70. * @data: Data to pass to the called function.
  71. *
  72. * This performs a depth-first traversal of the tree, calling the
  73. * function passed for each node. It calls the function for parents
  74. * before children.
  75. */
  76. static int for_each_padev(int (*fn)(struct device *, void *), void * data)
  77. {
  78. struct recurse_struct recurse_data = {
  79. .obj = data,
  80. .fn = fn,
  81. };
  82. return device_for_each_child(&root, &recurse_data, descend_children);
  83. }
  84. /**
  85. * match_device - Report whether this driver can handle this device
  86. * @driver: the PA-RISC driver to try
  87. * @dev: the PA-RISC device to try
  88. */
  89. static int match_device(const struct parisc_driver *driver, struct parisc_device *dev)
  90. {
  91. const struct parisc_device_id *ids;
  92. for (ids = driver->id_table; ids->sversion; ids++) {
  93. if ((ids->sversion != SVERSION_ANY_ID) &&
  94. (ids->sversion != dev->id.sversion))
  95. continue;
  96. if ((ids->hw_type != HWTYPE_ANY_ID) &&
  97. (ids->hw_type != dev->id.hw_type))
  98. continue;
  99. if ((ids->hversion != HVERSION_ANY_ID) &&
  100. (ids->hversion != dev->id.hversion))
  101. continue;
  102. return 1;
  103. }
  104. return 0;
  105. }
  106. static int parisc_driver_probe(struct device *dev)
  107. {
  108. int rc;
  109. struct parisc_device *pa_dev = to_parisc_device(dev);
  110. struct parisc_driver *pa_drv = to_parisc_driver(dev->driver);
  111. rc = pa_drv->probe(pa_dev);
  112. if (!rc)
  113. pa_dev->driver = pa_drv;
  114. return rc;
  115. }
  116. static void __exit parisc_driver_remove(struct device *dev)
  117. {
  118. struct parisc_device *pa_dev = to_parisc_device(dev);
  119. struct parisc_driver *pa_drv = to_parisc_driver(dev->driver);
  120. if (pa_drv->remove)
  121. pa_drv->remove(pa_dev);
  122. }
  123. /**
  124. * register_parisc_driver - Register this driver if it can handle a device
  125. * @driver: the PA-RISC driver to try
  126. */
  127. int register_parisc_driver(struct parisc_driver *driver)
  128. {
  129. /* FIXME: we need this because apparently the sti
  130. * driver can be registered twice */
  131. if (driver->drv.name) {
  132. pr_warn("BUG: skipping previously registered driver %s\n",
  133. driver->name);
  134. return 1;
  135. }
  136. if (!driver->probe) {
  137. pr_warn("BUG: driver %s has no probe routine\n", driver->name);
  138. return 1;
  139. }
  140. driver->drv.bus = &parisc_bus_type;
  141. /* We install our own probe and remove routines */
  142. WARN_ON(driver->drv.probe != NULL);
  143. WARN_ON(driver->drv.remove != NULL);
  144. driver->drv.name = driver->name;
  145. return driver_register(&driver->drv);
  146. }
  147. EXPORT_SYMBOL(register_parisc_driver);
  148. struct match_count {
  149. struct parisc_driver * driver;
  150. int count;
  151. };
  152. static int match_and_count(struct device * dev, void * data)
  153. {
  154. struct match_count * m = data;
  155. struct parisc_device * pdev = to_parisc_device(dev);
  156. if (check_dev(dev)) {
  157. if (match_device(m->driver, pdev))
  158. m->count++;
  159. }
  160. return 0;
  161. }
  162. /**
  163. * count_parisc_driver - count # of devices this driver would match
  164. * @driver: the PA-RISC driver to try
  165. *
  166. * Use by IOMMU support to "guess" the right size IOPdir.
  167. * Formula is something like memsize/(num_iommu * entry_size).
  168. */
  169. int __init count_parisc_driver(struct parisc_driver *driver)
  170. {
  171. struct match_count m = {
  172. .driver = driver,
  173. .count = 0,
  174. };
  175. for_each_padev(match_and_count, &m);
  176. return m.count;
  177. }
  178. /**
  179. * unregister_parisc_driver - Unregister this driver from the list of drivers
  180. * @driver: the PA-RISC driver to unregister
  181. */
  182. int unregister_parisc_driver(struct parisc_driver *driver)
  183. {
  184. driver_unregister(&driver->drv);
  185. return 0;
  186. }
  187. EXPORT_SYMBOL(unregister_parisc_driver);
  188. struct find_data {
  189. unsigned long hpa;
  190. struct parisc_device * dev;
  191. };
  192. static int find_device(struct device * dev, void * data)
  193. {
  194. struct parisc_device * pdev = to_parisc_device(dev);
  195. struct find_data * d = (struct find_data*)data;
  196. if (check_dev(dev)) {
  197. if (pdev->hpa.start == d->hpa) {
  198. d->dev = pdev;
  199. return 1;
  200. }
  201. }
  202. return 0;
  203. }
  204. static struct parisc_device *find_device_by_addr(unsigned long hpa)
  205. {
  206. struct find_data d = {
  207. .hpa = hpa,
  208. };
  209. int ret;
  210. ret = for_each_padev(find_device, &d);
  211. return ret ? d.dev : NULL;
  212. }
  213. static int __init is_IKE_device(struct device *dev, void *data)
  214. {
  215. struct parisc_device *pdev = to_parisc_device(dev);
  216. if (!check_dev(dev))
  217. return 0;
  218. if (pdev->id.hw_type != HPHW_BCPORT)
  219. return 0;
  220. if (IS_IKE(pdev) ||
  221. (pdev->id.hversion == REO_MERCED_PORT) ||
  222. (pdev->id.hversion == REOG_MERCED_PORT)) {
  223. return 1;
  224. }
  225. return 0;
  226. }
  227. int __init machine_has_merced_bus(void)
  228. {
  229. int ret;
  230. ret = for_each_padev(is_IKE_device, NULL);
  231. return ret ? 1 : 0;
  232. }
  233. /**
  234. * find_pa_parent_type - Find a parent of a specific type
  235. * @padev: The device to start searching from
  236. * @type: The device type to search for.
  237. *
  238. * Walks up the device tree looking for a device of the specified type.
  239. * If it finds it, it returns it. If not, it returns NULL.
  240. */
  241. const struct parisc_device *
  242. find_pa_parent_type(const struct parisc_device *padev, int type)
  243. {
  244. const struct device *dev = &padev->dev;
  245. while (dev != &root) {
  246. struct parisc_device *candidate = to_parisc_device(dev);
  247. if (candidate->id.hw_type == type)
  248. return candidate;
  249. dev = dev->parent;
  250. }
  251. return NULL;
  252. }
  253. /*
  254. * get_node_path fills in @path with the firmware path to the device.
  255. * Note that if @node is a parisc device, we don't fill in the 'mod' field.
  256. * This is because both callers pass the parent and fill in the mod
  257. * themselves. If @node is a PCI device, we do fill it in, even though this
  258. * is inconsistent.
  259. */
  260. static void get_node_path(struct device *dev, struct hardware_path *path)
  261. {
  262. int i = 5;
  263. memset(&path->bc, -1, 6);
  264. if (dev_is_pci(dev)) {
  265. unsigned int devfn = to_pci_dev(dev)->devfn;
  266. path->mod = PCI_FUNC(devfn);
  267. path->bc[i--] = PCI_SLOT(devfn);
  268. dev = dev->parent;
  269. }
  270. while (dev != &root) {
  271. if (dev_is_pci(dev)) {
  272. unsigned int devfn = to_pci_dev(dev)->devfn;
  273. path->bc[i--] = PCI_SLOT(devfn) | (PCI_FUNC(devfn)<< 5);
  274. } else if (dev->bus == &parisc_bus_type) {
  275. path->bc[i--] = to_parisc_device(dev)->hw_path;
  276. }
  277. dev = dev->parent;
  278. }
  279. }
  280. static char *print_hwpath(struct hardware_path *path, char *output)
  281. {
  282. int i;
  283. for (i = 0; i < 6; i++) {
  284. if (path->bc[i] == -1)
  285. continue;
  286. output += sprintf(output, "%u/", (unsigned char) path->bc[i]);
  287. }
  288. output += sprintf(output, "%u", (unsigned char) path->mod);
  289. return output;
  290. }
  291. /**
  292. * print_pa_hwpath - Returns hardware path for PA devices
  293. * @dev: The device to return the path for
  294. * @output: Pointer to a previously-allocated array to place the path in.
  295. *
  296. * This function fills in the output array with a human-readable path
  297. * to a PA device. This string is compatible with that used by PDC, and
  298. * may be printed on the outside of the box.
  299. */
  300. char *print_pa_hwpath(struct parisc_device *dev, char *output)
  301. {
  302. struct hardware_path path;
  303. get_node_path(dev->dev.parent, &path);
  304. path.mod = dev->hw_path;
  305. return print_hwpath(&path, output);
  306. }
  307. EXPORT_SYMBOL(print_pa_hwpath);
  308. #if defined(CONFIG_PCI) || defined(CONFIG_ISA)
  309. /**
  310. * get_pci_node_path - Determines the hardware path for a PCI device
  311. * @pdev: The device to return the path for
  312. * @path: Pointer to a previously-allocated array to place the path in.
  313. *
  314. * This function fills in the hardware_path structure with the route to
  315. * the specified PCI device. This structure is suitable for passing to
  316. * PDC calls.
  317. */
  318. void get_pci_node_path(struct pci_dev *pdev, struct hardware_path *path)
  319. {
  320. get_node_path(&pdev->dev, path);
  321. }
  322. EXPORT_SYMBOL(get_pci_node_path);
  323. /**
  324. * print_pci_hwpath - Returns hardware path for PCI devices
  325. * @dev: The device to return the path for
  326. * @output: Pointer to a previously-allocated array to place the path in.
  327. *
  328. * This function fills in the output array with a human-readable path
  329. * to a PCI device. This string is compatible with that used by PDC, and
  330. * may be printed on the outside of the box.
  331. */
  332. char *print_pci_hwpath(struct pci_dev *dev, char *output)
  333. {
  334. struct hardware_path path;
  335. get_pci_node_path(dev, &path);
  336. return print_hwpath(&path, output);
  337. }
  338. EXPORT_SYMBOL(print_pci_hwpath);
  339. #endif /* defined(CONFIG_PCI) || defined(CONFIG_ISA) */
  340. static void setup_bus_id(struct parisc_device *padev)
  341. {
  342. struct hardware_path path;
  343. char name[28];
  344. char *output = name;
  345. int i;
  346. get_node_path(padev->dev.parent, &path);
  347. for (i = 0; i < 6; i++) {
  348. if (path.bc[i] == -1)
  349. continue;
  350. output += sprintf(output, "%u:", (unsigned char) path.bc[i]);
  351. }
  352. sprintf(output, "%u", (unsigned char) padev->hw_path);
  353. dev_set_name(&padev->dev, name);
  354. }
  355. static struct parisc_device * __init create_tree_node(char id,
  356. struct device *parent)
  357. {
  358. struct parisc_device *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  359. if (!dev)
  360. return NULL;
  361. dev->hw_path = id;
  362. dev->id.hw_type = HPHW_FAULTY;
  363. dev->dev.parent = parent;
  364. setup_bus_id(dev);
  365. dev->dev.bus = &parisc_bus_type;
  366. dev->dma_mask = 0xffffffffUL; /* PARISC devices are 32-bit */
  367. /* make the generic dma mask a pointer to the parisc one */
  368. dev->dev.dma_mask = &dev->dma_mask;
  369. dev->dev.coherent_dma_mask = dev->dma_mask;
  370. if (device_register(&dev->dev)) {
  371. kfree(dev);
  372. return NULL;
  373. }
  374. return dev;
  375. }
  376. struct match_id_data {
  377. char id;
  378. struct parisc_device * dev;
  379. };
  380. static int match_by_id(struct device * dev, void * data)
  381. {
  382. struct parisc_device * pdev = to_parisc_device(dev);
  383. struct match_id_data * d = data;
  384. if (pdev->hw_path == d->id) {
  385. d->dev = pdev;
  386. return 1;
  387. }
  388. return 0;
  389. }
  390. /**
  391. * alloc_tree_node - returns a device entry in the iotree
  392. * @parent: the parent node in the tree
  393. * @id: the element of the module path for this entry
  394. *
  395. * Checks all the children of @parent for a matching @id. If none
  396. * found, it allocates a new device and returns it.
  397. */
  398. static struct parisc_device * __init alloc_tree_node(
  399. struct device *parent, char id)
  400. {
  401. struct match_id_data d = {
  402. .id = id,
  403. };
  404. if (device_for_each_child(parent, &d, match_by_id))
  405. return d.dev;
  406. else
  407. return create_tree_node(id, parent);
  408. }
  409. static struct parisc_device *create_parisc_device(struct hardware_path *modpath)
  410. {
  411. int i;
  412. struct device *parent = &root;
  413. for (i = 0; i < 6; i++) {
  414. if (modpath->bc[i] == -1)
  415. continue;
  416. parent = &alloc_tree_node(parent, modpath->bc[i])->dev;
  417. }
  418. return alloc_tree_node(parent, modpath->mod);
  419. }
  420. struct parisc_device * __init
  421. alloc_pa_dev(unsigned long hpa, struct hardware_path *mod_path)
  422. {
  423. int status;
  424. unsigned long bytecnt;
  425. u8 iodc_data[32];
  426. struct parisc_device *dev;
  427. const char *name;
  428. /* Check to make sure this device has not already been added - Ryan */
  429. if (find_device_by_addr(hpa) != NULL)
  430. return NULL;
  431. status = pdc_iodc_read(&bytecnt, hpa, 0, &iodc_data, 32);
  432. if (status != PDC_OK)
  433. return NULL;
  434. dev = create_parisc_device(mod_path);
  435. if (dev->id.hw_type != HPHW_FAULTY) {
  436. pr_err("Two devices have hardware path [%s]. IODC data for second device: %7phN\n"
  437. "Rearranging GSC cards sometimes helps\n",
  438. parisc_pathname(dev), iodc_data);
  439. return NULL;
  440. }
  441. dev->id.hw_type = iodc_data[3] & 0x1f;
  442. dev->id.hversion = (iodc_data[0] << 4) | ((iodc_data[1] & 0xf0) >> 4);
  443. dev->id.hversion_rev = iodc_data[1] & 0x0f;
  444. dev->id.sversion = ((iodc_data[4] & 0x0f) << 16) |
  445. (iodc_data[5] << 8) | iodc_data[6];
  446. dev->hpa.start = hpa;
  447. /* This is awkward. The STI spec says that gfx devices may occupy
  448. * 32MB or 64MB. Unfortunately, we don't know how to tell whether
  449. * it's the former or the latter. Assumptions either way can hurt us.
  450. */
  451. if (hpa == 0xf4000000 || hpa == 0xf8000000) {
  452. dev->hpa.end = hpa + 0x03ffffff;
  453. } else if (hpa == 0xf6000000 || hpa == 0xfa000000) {
  454. dev->hpa.end = hpa + 0x01ffffff;
  455. } else {
  456. dev->hpa.end = hpa + 0xfff;
  457. }
  458. dev->hpa.flags = IORESOURCE_MEM;
  459. dev->hpa.name = dev->name;
  460. name = parisc_hardware_description(&dev->id) ? : "unknown";
  461. snprintf(dev->name, sizeof(dev->name), "%s [%s]",
  462. name, parisc_pathname(dev));
  463. /* Silently fail things like mouse ports which are subsumed within
  464. * the keyboard controller
  465. */
  466. if ((hpa & 0xfff) == 0 && insert_resource(&iomem_resource, &dev->hpa))
  467. pr_warn("Unable to claim HPA %lx for device %s\n", hpa, name);
  468. return dev;
  469. }
  470. static int parisc_generic_match(struct device *dev, const struct device_driver *drv)
  471. {
  472. return match_device(to_parisc_driver(drv), to_parisc_device(dev));
  473. }
  474. static ssize_t make_modalias(const struct device *dev, char *buf)
  475. {
  476. const struct parisc_device *padev = to_parisc_device(dev);
  477. const struct parisc_device_id *id = &padev->id;
  478. return sprintf(buf, "parisc:t%02Xhv%04Xrev%02Xsv%08X\n",
  479. (u8)id->hw_type, (u16)id->hversion, (u8)id->hversion_rev,
  480. (u32)id->sversion);
  481. }
  482. static int parisc_uevent(const struct device *dev, struct kobj_uevent_env *env)
  483. {
  484. const struct parisc_device *padev;
  485. char modalias[40];
  486. if (!dev)
  487. return -ENODEV;
  488. padev = to_parisc_device(dev);
  489. if (!padev)
  490. return -ENODEV;
  491. if (add_uevent_var(env, "PARISC_NAME=%s", padev->name))
  492. return -ENOMEM;
  493. make_modalias(dev, modalias);
  494. if (add_uevent_var(env, "MODALIAS=%s", modalias))
  495. return -ENOMEM;
  496. return 0;
  497. }
  498. #define pa_dev_attr(name, field, format_string) \
  499. static ssize_t name##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  500. { \
  501. struct parisc_device *padev = to_parisc_device(dev); \
  502. return sprintf(buf, format_string, padev->field); \
  503. } \
  504. static DEVICE_ATTR_RO(name);
  505. #define pa_dev_attr_id(field, format) pa_dev_attr(field, id.field, format)
  506. pa_dev_attr(irq, irq, "%u\n");
  507. pa_dev_attr_id(hw_type, "0x%02x\n");
  508. pa_dev_attr(rev, id.hversion_rev, "0x%x\n");
  509. pa_dev_attr_id(hversion, "0x%03x\n");
  510. pa_dev_attr_id(sversion, "0x%05x\n");
  511. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  512. {
  513. return make_modalias(dev, buf);
  514. }
  515. static DEVICE_ATTR_RO(modalias);
  516. static struct attribute *parisc_device_attrs[] = {
  517. &dev_attr_irq.attr,
  518. &dev_attr_hw_type.attr,
  519. &dev_attr_rev.attr,
  520. &dev_attr_hversion.attr,
  521. &dev_attr_sversion.attr,
  522. &dev_attr_modalias.attr,
  523. NULL,
  524. };
  525. ATTRIBUTE_GROUPS(parisc_device);
  526. const struct bus_type parisc_bus_type = {
  527. .name = "parisc",
  528. .match = parisc_generic_match,
  529. .uevent = parisc_uevent,
  530. .dev_groups = parisc_device_groups,
  531. .probe = parisc_driver_probe,
  532. .remove = __exit_p(parisc_driver_remove),
  533. };
  534. /**
  535. * register_parisc_device - Locate a driver to manage this device.
  536. * @dev: The parisc device.
  537. *
  538. * Search the driver list for a driver that is willing to manage
  539. * this device.
  540. */
  541. int __init register_parisc_device(struct parisc_device *dev)
  542. {
  543. if (!dev)
  544. return 0;
  545. if (dev->driver)
  546. return 1;
  547. return 0;
  548. }
  549. /**
  550. * match_pci_device - Matches a pci device against a given hardware path
  551. * entry.
  552. * @dev: the generic device (known to be contained by a pci_dev).
  553. * @index: the current BC index
  554. * @modpath: the hardware path.
  555. * @return: true if the device matches the hardware path.
  556. */
  557. static int match_pci_device(struct device *dev, int index,
  558. struct hardware_path *modpath)
  559. {
  560. struct pci_dev *pdev = to_pci_dev(dev);
  561. int id;
  562. if (index == 5) {
  563. /* we are at the end of the path, and on the actual device */
  564. unsigned int devfn = pdev->devfn;
  565. return ((modpath->bc[5] == PCI_SLOT(devfn)) &&
  566. (modpath->mod == PCI_FUNC(devfn)));
  567. }
  568. /* index might be out of bounds for bc[] */
  569. if (index >= 6)
  570. return 0;
  571. id = PCI_SLOT(pdev->devfn) | (PCI_FUNC(pdev->devfn) << 5);
  572. return (modpath->bc[index] == id);
  573. }
  574. /**
  575. * match_parisc_device - Matches a parisc device against a given hardware
  576. * path entry.
  577. * @dev: the generic device (known to be contained by a parisc_device).
  578. * @index: the current BC index
  579. * @modpath: the hardware path.
  580. * @return: true if the device matches the hardware path.
  581. */
  582. static int match_parisc_device(struct device *dev, int index,
  583. struct hardware_path *modpath)
  584. {
  585. struct parisc_device *curr = to_parisc_device(dev);
  586. char id = (index == 6) ? modpath->mod : modpath->bc[index];
  587. return (curr->hw_path == id);
  588. }
  589. struct parse_tree_data {
  590. int index;
  591. struct hardware_path * modpath;
  592. struct device * dev;
  593. };
  594. static int check_parent(struct device * dev, void * data)
  595. {
  596. struct parse_tree_data * d = data;
  597. if (check_dev(dev)) {
  598. if (dev->bus == &parisc_bus_type) {
  599. if (match_parisc_device(dev, d->index, d->modpath))
  600. d->dev = dev;
  601. } else if (dev_is_pci(dev)) {
  602. if (match_pci_device(dev, d->index, d->modpath))
  603. d->dev = dev;
  604. } else if (dev->bus == NULL) {
  605. /* we are on a bus bridge */
  606. struct device *new = parse_tree_node(dev, d->index, d->modpath);
  607. if (new)
  608. d->dev = new;
  609. }
  610. }
  611. return d->dev != NULL;
  612. }
  613. /**
  614. * parse_tree_node - returns a device entry in the iotree
  615. * @parent: the parent node in the tree
  616. * @index: the current BC index
  617. * @modpath: the hardware_path struct to match a device against
  618. * @return: The corresponding device if found, NULL otherwise.
  619. *
  620. * Checks all the children of @parent for a matching @id. If none
  621. * found, it returns NULL.
  622. */
  623. static struct device *
  624. parse_tree_node(struct device *parent, int index, struct hardware_path *modpath)
  625. {
  626. struct parse_tree_data d = {
  627. .index = index,
  628. .modpath = modpath,
  629. };
  630. struct recurse_struct recurse_data = {
  631. .obj = &d,
  632. .fn = check_parent,
  633. };
  634. if (device_for_each_child(parent, &recurse_data, descend_children))
  635. { /* nothing */ }
  636. return d.dev;
  637. }
  638. /**
  639. * hwpath_to_device - Finds the generic device corresponding to a given hardware path.
  640. * @modpath: the hardware path.
  641. * @return: The target device, NULL if not found.
  642. */
  643. struct device *hwpath_to_device(struct hardware_path *modpath)
  644. {
  645. int i;
  646. struct device *parent = &root;
  647. for (i = 0; i < 6; i++) {
  648. if (modpath->bc[i] == -1)
  649. continue;
  650. parent = parse_tree_node(parent, i, modpath);
  651. if (!parent)
  652. return NULL;
  653. }
  654. if (dev_is_pci(parent)) /* pci devices already parse MOD */
  655. return parent;
  656. else
  657. return parse_tree_node(parent, 6, modpath);
  658. }
  659. EXPORT_SYMBOL(hwpath_to_device);
  660. /**
  661. * device_to_hwpath - Populates the hwpath corresponding to the given device.
  662. * @dev: the target device
  663. * @path: pointer to a previously allocated hwpath struct to be filled in
  664. */
  665. void device_to_hwpath(struct device *dev, struct hardware_path *path)
  666. {
  667. struct parisc_device *padev;
  668. if (dev->bus == &parisc_bus_type) {
  669. padev = to_parisc_device(dev);
  670. get_node_path(dev->parent, path);
  671. path->mod = padev->hw_path;
  672. } else if (dev_is_pci(dev)) {
  673. get_node_path(dev, path);
  674. }
  675. }
  676. EXPORT_SYMBOL(device_to_hwpath);
  677. #define BC_PORT_MASK 0x8
  678. #define BC_LOWER_PORT 0x8
  679. #define BUS_CONVERTER(dev) \
  680. ((dev->id.hw_type == HPHW_IOA) || (dev->id.hw_type == HPHW_BCPORT))
  681. #define IS_LOWER_PORT(dev) \
  682. ((gsc_readl(dev->hpa.start + offsetof(struct bc_module, io_status)) \
  683. & BC_PORT_MASK) == BC_LOWER_PORT)
  684. #define MAX_NATIVE_DEVICES 64
  685. #define NATIVE_DEVICE_OFFSET 0x1000
  686. #define FLEX_MASK F_EXTEND(0xfffc0000)
  687. #define IO_IO_LOW offsetof(struct bc_module, io_io_low)
  688. #define IO_IO_HIGH offsetof(struct bc_module, io_io_high)
  689. #define READ_IO_IO_LOW(dev) (unsigned long)(signed int)gsc_readl(dev->hpa.start + IO_IO_LOW)
  690. #define READ_IO_IO_HIGH(dev) (unsigned long)(signed int)gsc_readl(dev->hpa.start + IO_IO_HIGH)
  691. static void walk_native_bus(unsigned long io_io_low, unsigned long io_io_high,
  692. struct device *parent);
  693. static void __init walk_lower_bus(struct parisc_device *dev)
  694. {
  695. unsigned long io_io_low, io_io_high;
  696. if (!BUS_CONVERTER(dev) || IS_LOWER_PORT(dev))
  697. return;
  698. if (dev->id.hw_type == HPHW_IOA) {
  699. io_io_low = (unsigned long)(signed int)(READ_IO_IO_LOW(dev) << 16);
  700. io_io_high = io_io_low + MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET;
  701. } else {
  702. io_io_low = (READ_IO_IO_LOW(dev) + ~FLEX_MASK) & FLEX_MASK;
  703. io_io_high = (READ_IO_IO_HIGH(dev)+ ~FLEX_MASK) & FLEX_MASK;
  704. }
  705. walk_native_bus(io_io_low, io_io_high, &dev->dev);
  706. }
  707. /**
  708. * walk_native_bus -- Probe a bus for devices
  709. * @io_io_low: Base address of this bus.
  710. * @io_io_high: Last address of this bus.
  711. * @parent: The parent bus device.
  712. *
  713. * A native bus (eg Runway or GSC) may have up to 64 devices on it,
  714. * spaced at intervals of 0x1000 bytes. PDC may not inform us of these
  715. * devices, so we have to probe for them. Unfortunately, we may find
  716. * devices which are not physically connected (such as extra serial &
  717. * keyboard ports). This problem is not yet solved.
  718. */
  719. static void __init walk_native_bus(unsigned long io_io_low,
  720. unsigned long io_io_high, struct device *parent)
  721. {
  722. int i, devices_found = 0;
  723. unsigned long hpa = io_io_low;
  724. struct hardware_path path;
  725. get_node_path(parent, &path);
  726. do {
  727. for(i = 0; i < MAX_NATIVE_DEVICES; i++, hpa += NATIVE_DEVICE_OFFSET) {
  728. struct parisc_device *dev;
  729. /* Was the device already added by Firmware? */
  730. dev = find_device_by_addr(hpa);
  731. if (!dev) {
  732. path.mod = i;
  733. dev = alloc_pa_dev(hpa, &path);
  734. if (!dev)
  735. continue;
  736. register_parisc_device(dev);
  737. devices_found++;
  738. }
  739. walk_lower_bus(dev);
  740. }
  741. } while(!devices_found && hpa < io_io_high);
  742. }
  743. #define CENTRAL_BUS_ADDR F_EXTEND(0xfff80000)
  744. /**
  745. * walk_central_bus - Find devices attached to the central bus
  746. *
  747. * PDC doesn't tell us about all devices in the system. This routine
  748. * finds devices connected to the central bus.
  749. */
  750. void __init walk_central_bus(void)
  751. {
  752. walk_native_bus(CENTRAL_BUS_ADDR,
  753. CENTRAL_BUS_ADDR + (MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET),
  754. &root);
  755. }
  756. static __init void print_parisc_device(struct parisc_device *dev)
  757. {
  758. static int count __initdata;
  759. pr_info("%d. %s at %pap { type:%d, hv:%#x, sv:%#x, rev:%#x }",
  760. ++count, dev->name, &(dev->hpa.start), dev->id.hw_type,
  761. dev->id.hversion, dev->id.sversion, dev->id.hversion_rev);
  762. if (dev->num_addrs) {
  763. int k;
  764. pr_cont(", additional addresses: ");
  765. for (k = 0; k < dev->num_addrs; k++)
  766. pr_cont("0x%lx ", dev->addr[k]);
  767. }
  768. pr_cont("\n");
  769. }
  770. /**
  771. * init_parisc_bus - Some preparation to be done before inventory
  772. */
  773. void __init init_parisc_bus(void)
  774. {
  775. if (bus_register(&parisc_bus_type))
  776. panic("Could not register PA-RISC bus type\n");
  777. if (device_register(&root))
  778. panic("Could not register PA-RISC root device\n");
  779. get_device(&root);
  780. }
  781. static __init void qemu_header(void)
  782. {
  783. int num;
  784. unsigned long *p;
  785. pr_info("--- cut here ---\n");
  786. pr_info("/* AUTO-GENERATED HEADER FILE FOR SEABIOS FIRMWARE */\n");
  787. pr_cont("/* generated with Linux kernel */\n");
  788. pr_cont("/* search for PARISC_QEMU_MACHINE_HEADER in Linux */\n\n");
  789. pr_info("#define PARISC_MODEL \"%s\"\n\n",
  790. boot_cpu_data.pdc.sys_model_name);
  791. #define p ((unsigned long *)&boot_cpu_data.pdc.model)
  792. pr_info("#define PARISC_PDC_MODEL 0x%lx, 0x%lx, 0x%lx, "
  793. "0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx\n\n",
  794. p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9]);
  795. #undef p
  796. pr_info("#define PARISC_PDC_VERSION 0x%04lx\n\n",
  797. boot_cpu_data.pdc.versions);
  798. pr_info("#define PARISC_PDC_CPUID 0x%04lx\n\n",
  799. boot_cpu_data.pdc.cpuid);
  800. pr_info("#define PARISC_PDC_CAPABILITIES 0x%04lx\n\n",
  801. boot_cpu_data.pdc.capabilities);
  802. pr_info("#define PARISC_PDC_ENTRY_ORG 0x%04lx\n\n",
  803. #ifdef CONFIG_64BIT
  804. (unsigned long)(PAGE0->mem_pdc_hi) << 32 |
  805. #endif
  806. (unsigned long)PAGE0->mem_pdc);
  807. pr_info("#define PARISC_PDC_CACHE_INFO");
  808. p = (unsigned long *) &cache_info;
  809. for (num = 0; num < sizeof(cache_info); num += sizeof(unsigned long)) {
  810. if (((num % 5) == 0)) {
  811. pr_cont(" \\\n");
  812. pr_info("\t");
  813. }
  814. pr_cont("%s0x%04lx",
  815. num?", ":"", *p++);
  816. }
  817. pr_cont("\n\n");
  818. }
  819. static __init int qemu_print_hpa(struct device *lin_dev, void *data)
  820. {
  821. struct parisc_device *dev = to_parisc_device(lin_dev);
  822. unsigned long hpa = dev->hpa.start;
  823. pr_cont("\t{\t.hpa = 0x%08lx,\\\n", hpa);
  824. pr_cont("\t\t.iodc = &iodc_data_hpa_%08lx,\\\n", hpa);
  825. pr_cont("\t\t.mod_info = &mod_info_hpa_%08lx,\\\n", hpa);
  826. pr_cont("\t\t.mod_path = &mod_path_hpa_%08lx,\\\n", hpa);
  827. pr_cont("\t\t.num_addr = HPA_%08lx_num_addr,\\\n", hpa);
  828. pr_cont("\t\t.add_addr = { HPA_%08lx_add_addr } },\\\n", hpa);
  829. return 0;
  830. }
  831. static __init void qemu_footer(void)
  832. {
  833. pr_info("\n\n#define PARISC_DEVICE_LIST \\\n");
  834. for_each_padev(qemu_print_hpa, NULL);
  835. pr_cont("\t{ 0, }\n");
  836. pr_info("--- cut here ---\n");
  837. }
  838. /* print iodc data of the various hpa modules for qemu inclusion */
  839. static __init int qemu_print_iodc_data(struct device *lin_dev, void *data)
  840. {
  841. struct parisc_device *dev = to_parisc_device(lin_dev);
  842. unsigned long count;
  843. unsigned long hpa = dev->hpa.start;
  844. int status;
  845. struct pdc_iodc iodc_data;
  846. int mod_index;
  847. struct pdc_system_map_mod_info pdc_mod_info;
  848. struct pdc_module_path mod_path;
  849. status = pdc_iodc_read(&count, hpa, 0,
  850. &iodc_data, sizeof(iodc_data));
  851. if (status != PDC_OK) {
  852. pr_info("No IODC data for hpa 0x%08lx\n", hpa);
  853. return 0;
  854. }
  855. pr_info("\n");
  856. /* Prevent hung task messages when printing on serial console */
  857. cond_resched();
  858. pr_info("#define HPA_%08lx_DESCRIPTION \"%s\"\n",
  859. hpa, parisc_hardware_description(&dev->id));
  860. mod_index = 0;
  861. do {
  862. status = pdc_system_map_find_mods(&pdc_mod_info,
  863. &mod_path, mod_index++);
  864. } while (status == PDC_OK && pdc_mod_info.mod_addr != hpa);
  865. pr_info("static struct pdc_system_map_mod_info"
  866. " mod_info_hpa_%08lx = {\n", hpa);
  867. #define DO(member) \
  868. pr_cont("\t." #member " = 0x%x,\n", \
  869. (unsigned int)pdc_mod_info.member)
  870. DO(mod_addr);
  871. DO(mod_pgs);
  872. DO(add_addrs);
  873. pr_cont("};\n");
  874. #undef DO
  875. pr_info("static struct pdc_module_path "
  876. "mod_path_hpa_%08lx = {\n", hpa);
  877. pr_cont("\t.path = { ");
  878. pr_cont(".flags = 0x%x, ", mod_path.path.flags);
  879. pr_cont(".bc = { 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x }, ",
  880. (unsigned char)mod_path.path.bc[0],
  881. (unsigned char)mod_path.path.bc[1],
  882. (unsigned char)mod_path.path.bc[2],
  883. (unsigned char)mod_path.path.bc[3],
  884. (unsigned char)mod_path.path.bc[4],
  885. (unsigned char)mod_path.path.bc[5]);
  886. pr_cont(".mod = 0x%x ", mod_path.path.mod);
  887. pr_cont(" },\n");
  888. pr_cont("\t.layers = { 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x }\n",
  889. mod_path.layers[0], mod_path.layers[1], mod_path.layers[2],
  890. mod_path.layers[3], mod_path.layers[4], mod_path.layers[5]);
  891. pr_cont("};\n");
  892. pr_info("static struct pdc_iodc iodc_data_hpa_%08lx = {\n", hpa);
  893. #define DO(member) \
  894. pr_cont("\t." #member " = 0x%04lx,\n", \
  895. (unsigned long)iodc_data.member)
  896. DO(hversion_model);
  897. DO(hversion);
  898. DO(spa);
  899. DO(type);
  900. DO(sversion_rev);
  901. DO(sversion_model);
  902. DO(sversion_opt);
  903. DO(rev);
  904. DO(dep);
  905. DO(features);
  906. DO(checksum);
  907. DO(length);
  908. #undef DO
  909. pr_cont("\t/* pad: 0x%04x, 0x%04x */\n",
  910. iodc_data.pad[0], iodc_data.pad[1]);
  911. pr_cont("};\n");
  912. pr_info("#define HPA_%08lx_num_addr %d\n", hpa, dev->num_addrs);
  913. pr_info("#define HPA_%08lx_add_addr ", hpa);
  914. count = 0;
  915. if (dev->num_addrs == 0)
  916. pr_cont("0");
  917. while (count < dev->num_addrs) {
  918. pr_cont("0x%08lx, ", dev->addr[count]);
  919. count++;
  920. }
  921. pr_cont("\n\n");
  922. return 0;
  923. }
  924. static __init int print_one_device(struct device * dev, void * data)
  925. {
  926. struct parisc_device * pdev = to_parisc_device(dev);
  927. if (check_dev(dev))
  928. print_parisc_device(pdev);
  929. return 0;
  930. }
  931. /**
  932. * print_parisc_devices - Print out a list of devices found in this system
  933. */
  934. void __init print_parisc_devices(void)
  935. {
  936. for_each_padev(print_one_device, NULL);
  937. #define PARISC_QEMU_MACHINE_HEADER 0
  938. if (PARISC_QEMU_MACHINE_HEADER) {
  939. qemu_header();
  940. for_each_padev(qemu_print_iodc_data, NULL);
  941. qemu_footer();
  942. }
  943. }