OSDN Git Service

Fix build on NetBSD-4.
[android-x86/external-libpciaccess.git] / src / solx_devfs.c
1 /*
2  * (C) Copyright IBM Corporation 2006
3  * Copyright (c) 2007, 2009, 2011, 2012, 2013 Oracle and/or its affiliates.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * on the rights to use, copy, modify, merge, publish, distribute, sub
10  * license, and/or sell copies of the Software, and to permit persons to whom
11  * the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
20  * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 /*
26  * Solaris devfs interfaces
27  */
28
29 #include <stdlib.h>
30 #include <strings.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <fcntl.h>
35 #include <sys/mman.h>
36 #include <errno.h>
37 #include <sys/pci.h>
38 #include <libdevinfo.h>
39 #include "pci_tools.h"
40
41 #ifdef __x86
42 # include <sys/sysi86.h>
43 # include <sys/psw.h>
44 #endif
45
46 #include "pciaccess.h"
47 #include "pciaccess_private.h"
48
49 /* #define DEBUG */
50
51 #define INITIAL_NUM_DEVICES     256
52 #define CELL_NUMS_1275  (sizeof(pci_regspec_t) / sizeof(uint_t))
53
54 typedef struct i_devnode {
55     uint8_t bus;
56     uint8_t dev;
57     uint8_t func;
58     di_node_t node;
59 } i_devnode_t;
60
61 typedef struct nexus {
62     int fd;
63     int first_bus;
64     int last_bus;
65     int domain;
66     char *path;                 /* for errors/debugging; fd is all we need */
67     char *dev_path;
68     struct nexus *next;
69 } nexus_t;
70
71 typedef struct probe_info {
72     volatile size_t num_allocated_elems;
73     volatile size_t num_devices;
74     struct pci_device_private * volatile devices;
75 } probe_info_t;
76
77 typedef struct probe_args {
78     probe_info_t *pinfo;
79     nexus_t *nexus;
80     int ret;
81 } probe_args_t;
82
83 typedef struct property_info {
84     const char *name;
85     int value;
86 } property_info_t;
87
88 static nexus_t *nexus_list = NULL;
89 #if !defined(__sparc)
90 static int xsvc_fd = -1;
91 #endif
92
93 #ifdef __sparc
94 static di_prom_handle_t di_phdl;
95 static size_t  nexus_count = 0;
96 #endif
97
98 /*
99  * Read config space in native processor endianness.  Endian-neutral
100  * processing can then take place.  On big endian machines, MSB and LSB
101  * of little endian data end up switched if read as little endian.
102  * They are in correct order if read as big endian.
103  */
104 #if defined(__sparc)
105 # define NATIVE_ENDIAN  PCITOOL_ACC_ATTR_ENDN_BIG
106 #elif defined(__x86)
107 # define NATIVE_ENDIAN  PCITOOL_ACC_ATTR_ENDN_LTL
108 #else
109 # error "ISA is neither __sparc nor __x86"
110 #endif
111
112 #ifdef __sparc
113 #define MAPPING_DEV_PATH(dev)    (((struct pci_device_private *) dev)->device_string)
114 #endif
115
116 /*
117  * Identify problematic southbridges.  These have device id 0x5249 and
118  * vendor id 0x10b9.  Check for revision ID 0 and class code 060400 as well.
119  * Values are little endian, so they are reversed for SPARC.
120  *
121  * Check for these southbridges on all architectures, as the issue is a
122  * southbridge issue, independent of processor.
123  *
124  * If one of these is found during probing, skip probing other devs/funcs on
125  * the rest of the bus, since the southbridge and all devs underneath will
126  * otherwise disappear.
127  */
128 #if (NATIVE_ENDIAN == PCITOOL_ACC_ATTR_ENDN_BIG)
129 # define U45_SB_DEVID_VID       0xb9104952
130 # define U45_SB_CLASS_RID       0x00000406
131 #else
132 # define U45_SB_DEVID_VID       0x524910b9
133 # define U45_SB_CLASS_RID       0x06040000
134 #endif
135
136 static nexus_t *
137 find_nexus_for_bus( int domain, int bus )
138 {
139     nexus_t *nexus;
140
141     for (nexus = nexus_list ; nexus != NULL ; nexus = nexus->next) {
142         if ((domain == nexus->domain) &&
143             (bus >= nexus->first_bus) && (bus <= nexus->last_bus)) {
144             return nexus;
145         }
146     }
147     return NULL;
148 }
149
150 /*
151  * Release all the resources
152  * Solaris version
153  */
154 static void
155 pci_system_solx_devfs_destroy( void )
156 {
157     /*
158      * The memory allocated for pci_sys & devices in create routines
159      * will be freed in pci_system_cleanup.
160      * Need to free system-specific allocations here.
161      */
162     nexus_t *nexus, *next;
163
164     for (nexus = nexus_list ; nexus != NULL ; nexus = next) {
165         next = nexus->next;
166         close(nexus->fd);
167         free(nexus->path);
168         free(nexus->dev_path);
169         free(nexus);
170     }
171     nexus_list = NULL;
172
173 #ifdef __sparc
174     if (di_phdl != DI_PROM_HANDLE_NIL)
175         (void) di_prom_fini(di_phdl);
176 #else
177     if (xsvc_fd >= 0) {
178         close(xsvc_fd);
179         xsvc_fd = -1;
180     }
181 #endif
182 }
183
184
185 #ifdef __sparc
186 /*
187  * Release resources per device
188  */
189 static void
190 pci_system_solx_devfs_destroy_device( struct pci_device *dev )
191 {
192    if (MAPPING_DEV_PATH(dev))
193         di_devfs_path_free((char *) MAPPING_DEV_PATH(dev));
194 }
195 #endif
196
197
198 static int
199 probe_device_node(di_node_t node, void *arg)
200 {
201     int *retbuf = NULL;
202     int len = 0, i;
203     struct pci_device   *pci_base;
204     probe_info_t *pinfo = ((probe_args_t *)arg)->pinfo;
205     nexus_t *nexus = ((probe_args_t *)arg)->nexus;
206     property_info_t property_list[] = {
207         { "class-code", 0 },
208         { "device-id", 0 },
209         { "vendor-id", 0 },
210         { "revision-id", 0},
211         { "subsystem-vendor-id", 0},
212         { "subsystem-id", 0},
213     };
214 #define NUM_PROPERTIES          sizeof(property_list)/sizeof(property_info_t)
215
216     len = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "reg", &retbuf);
217
218 #ifdef __sparc
219     if ((len <= 0) && di_phdl)
220         len = di_prom_prop_lookup_ints(di_phdl, node, "reg", &retbuf);
221 #endif
222
223     /* Exclude usb devices */
224     if (len < 5) {
225         return DI_WALK_CONTINUE;
226     }
227
228     pci_base = &pinfo->devices[pinfo->num_devices].base;
229
230     pci_base->domain = nexus->domain;
231     pci_base->bus = PCI_REG_BUS_G(retbuf[0]);
232     pci_base->dev = PCI_REG_DEV_G(retbuf[0]);
233     pci_base->func  = PCI_REG_FUNC_G(retbuf[0]);
234
235     /* Get property values */
236     for (i = 0; i < NUM_PROPERTIES; i++) {
237         len = di_prop_lookup_ints(DDI_DEV_T_ANY, node,
238                 property_list[i].name, &retbuf);
239 #ifdef __sparc
240         if ((len <= 0) && di_phdl)
241             len = di_prom_prop_lookup_ints(di_phdl, node,
242                 property_list[i].name, &retbuf);
243 #endif
244
245         if (len > 0)
246             property_list[i].value = retbuf[0];
247         else {
248             /* a device must have property "class-code", "device-id", "vendor-id" */
249             if (i < 3)
250                 return DI_WALK_CONTINUE;
251 #ifdef DEBUG
252             fprintf(stderr, "cannot get property \"%s\" for nexus = %s :\n",
253                 property_list[i].name, nexus->path);
254             fprintf(stderr, "   domain = %x, busno = %x, devno = %x, funcno = %x\n",
255                 pci_base->domain, pci_base->bus, pci_base->dev, pci_base->func);
256 #endif
257         }
258     }
259
260     if ((property_list[1].value == 0) && (property_list[2].value == 0))
261         return DI_WALK_CONTINUE;
262
263     pci_base->device_class = property_list[0].value;
264     pci_base->device_id = property_list[1].value;
265     pci_base->vendor_id = property_list[2].value;
266     pci_base->revision = property_list[3].value;
267     pci_base->subvendor_id = property_list[4].value;
268     pci_base->subdevice_id = property_list[5].value;
269
270 #ifdef DEBUG
271     fprintf(stderr,
272             "nexus = %s, domain = %x, busno = %x, devno = %x, funcno = %x\n",
273             nexus->path, pci_base->domain, pci_base->bus, pci_base->dev, pci_base->func);
274 #endif
275
276     pinfo->num_devices++;
277     if (pinfo->num_devices == pinfo->num_allocated_elems) {
278         struct pci_device_private *new_devs;
279         size_t new_num_elems = pinfo->num_allocated_elems * 2;
280
281         new_devs = realloc(pinfo->devices,
282         new_num_elems * sizeof (struct pci_device_private));
283         if (new_devs == NULL) {
284             (void) fprintf(stderr,
285                    "Error allocating memory for PCI devices:"
286                    " %s\n discarding additional devices\n",
287                    strerror(errno));
288             ((probe_args_t *)arg)->ret = 1;
289             return (DI_WALK_TERMINATE);
290         }
291         (void) memset(&new_devs[pinfo->num_devices], 0,
292                 pinfo->num_allocated_elems *
293                 sizeof (struct pci_device_private));
294         pinfo->num_allocated_elems = new_num_elems;
295         pinfo->devices = new_devs;
296     }
297
298     return (DI_WALK_CONTINUE);
299 }
300 /*
301  * This function is called from di_walk_minor() when any PROBE is processed
302  */
303 static int
304 probe_nexus_node(di_node_t di_node, di_minor_t minor, void *arg)
305 {
306     probe_info_t *pinfo = (probe_info_t *)arg;
307     char *nexus_name, *nexus_dev_path;
308     nexus_t *nexus;
309     int fd;
310     char nexus_path[MAXPATHLEN];
311
312     di_prop_t prop;
313     char *strings;
314     int *ints;
315     int numval;
316     int pci_node = 0;
317     int first_bus = 0, last_bus = PCI_REG_BUS_G(PCI_REG_BUS_M);
318     int domain = 0;
319     di_node_t rnode =  DI_NODE_NIL;
320 #ifdef __sparc
321     int bus_range_found = 0;
322     int device_type_found = 0;
323     di_prom_prop_t prom_prop;
324 #endif
325
326
327 #ifdef DEBUG
328     nexus_name = di_devfs_minor_path(minor);
329     fprintf(stderr, "-- device name: %s\n", nexus_name);
330     di_devfs_path_free(nexus_name);
331 #endif
332
333     for (prop = di_prop_next(di_node, NULL); prop != NULL;
334          prop = di_prop_next(di_node, prop)) {
335
336         const char *prop_name = di_prop_name(prop);
337
338 #ifdef DEBUG
339         fprintf(stderr, "   property: %s\n", prop_name);
340 #endif
341
342         if (strcmp(prop_name, "device_type") == 0) {
343             numval = di_prop_strings(prop, &strings);
344             if (numval == 1) {
345                 if (strncmp(strings, "pci", 3) != 0)
346                     /* not a PCI node, bail */
347                     return (DI_WALK_CONTINUE);
348                 else {
349                     pci_node = 1;
350 #ifdef __sparc
351                     device_type_found =  1;
352 #endif
353                 }
354             }
355         }
356         else if (strcmp(prop_name, "class-code") == 0) {
357             /* not a root bus node, bail */
358             return (DI_WALK_CONTINUE);
359         }
360         else if (strcmp(prop_name, "bus-range") == 0) {
361             numval = di_prop_ints(prop, &ints);
362             if (numval == 2) {
363                 first_bus = ints[0];
364                 last_bus = ints[1];
365 #ifdef __sparc
366                 bus_range_found = 1;
367 #endif
368             }
369         }
370 #ifdef __sparc
371         domain = nexus_count;
372 #else
373         else if (strcmp(prop_name, "pciseg") == 0) {
374             numval = di_prop_ints(prop, &ints);
375             if (numval == 1) {
376                 domain = ints[0];
377             }
378         }
379 #endif
380     }
381
382 #ifdef __sparc
383     if ((!device_type_found) && di_phdl) {
384         numval = di_prom_prop_lookup_strings(di_phdl, di_node,
385             "device_type", &strings);
386         if (numval == 1) {
387             if (strncmp(strings, "pci", 3) != 0)
388                 return (DI_WALK_CONTINUE);
389             else
390                 pci_node = 1;
391         }
392     }
393
394     if ((!bus_range_found) && di_phdl) {
395         numval = di_prom_prop_lookup_ints(di_phdl, di_node,
396             "bus-range", &ints);
397         if (numval == 2) {
398             first_bus = ints[0];
399             last_bus = ints[1];
400         }
401     }
402 #endif
403
404     if (pci_node != 1)
405         return (DI_WALK_CONTINUE);
406
407     /* we have a PCI root bus node. */
408     nexus = calloc(1, sizeof(nexus_t));
409     if (nexus == NULL) {
410         (void) fprintf(stderr, "Error allocating memory for nexus: %s\n",
411                        strerror(errno));
412         return (DI_WALK_TERMINATE);
413     }
414     nexus->first_bus = first_bus;
415     nexus->last_bus = last_bus;
416     nexus->domain = domain;
417
418 #ifdef __sparc
419     nexus_count++;
420 #endif
421
422     nexus_name = di_devfs_minor_path(minor);
423     if (nexus_name == NULL) {
424         (void) fprintf(stderr, "Error getting nexus path: %s\n",
425                        strerror(errno));
426         free(nexus);
427         return (DI_WALK_CONTINUE);
428     }
429
430     snprintf(nexus_path, sizeof(nexus_path), "/devices%s", nexus_name);
431     di_devfs_path_free(nexus_name);
432
433 #ifdef DEBUG
434     fprintf(stderr, "nexus = %s, bus-range = %d - %d\n",
435             nexus_path, first_bus, last_bus);
436 #endif
437
438     if ((fd = open(nexus_path, O_RDWR | O_CLOEXEC)) >= 0) {
439         probe_args_t args;
440
441         nexus->fd = fd;
442         nexus->path = strdup(nexus_path);
443         nexus_dev_path = di_devfs_path(di_node);
444         nexus->dev_path = strdup(nexus_dev_path);
445         di_devfs_path_free(nexus_dev_path);
446
447         if ((rnode = di_init(nexus->dev_path, DINFOCPYALL)) == DI_NODE_NIL) {
448             (void) fprintf(stderr, "di_init failed: %s\n", strerror(errno));
449             close(nexus->fd);
450             free(nexus->path);
451             free(nexus->dev_path);
452             free(nexus);
453             return (DI_WALK_TERMINATE);
454         }
455
456         /* Walk through devices under the rnode */
457         args.pinfo = pinfo;
458         args.nexus = nexus;
459         args.ret = 0;
460
461         (void) di_walk_node(rnode, DI_WALK_CLDFIRST, (void *)&args, probe_device_node);
462         if (args.ret) {
463             close(nexus->fd);
464             free(nexus->path);
465             free(nexus->dev_path);
466             free(nexus);
467             di_fini(rnode);
468             return (DI_WALK_TERMINATE);
469         }
470
471         nexus->next = nexus_list;
472         nexus_list = nexus;
473     } else {
474         (void) fprintf(stderr, "Error opening %s: %s\n",
475                        nexus_path, strerror(errno));
476         free(nexus);
477     }
478
479     if (rnode != DI_NODE_NIL) {
480         di_fini(rnode);
481     }
482
483     return DI_WALK_CONTINUE;
484 }
485
486 static int
487 find_target_node(di_node_t node, void *arg)
488 {
489     int *regbuf = NULL;
490     int len = 0;
491     uint32_t busno, funcno, devno;
492     i_devnode_t *devnode = (i_devnode_t *)arg;
493
494     /*
495      * Test the property functions, only for testing
496      */
497     /*
498     void *prop = DI_PROP_NIL;
499
500     (void) fprintf(stderr, "start of node 0x%x\n", node->nodeid);
501     while ((prop = di_prop_hw_next(node, prop)) != DI_PROP_NIL) {
502         int i;
503         (void) fprintf(stderr, "name=%s: ", di_prop_name(prop));
504         len = 0;
505         if (!strcmp(di_prop_name(prop), "reg")) {
506             len = di_prop_ints(prop, &regbuf);
507         }
508         for (i = 0; i < len; i++) {
509             fprintf(stderr, "0x%0x.", regbuf[i]);
510         }
511         fprintf(stderr, "\n");
512     }
513     (void) fprintf(stderr, "end of node 0x%x\n", node->nodeid);
514     */
515
516     len = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "reg", &regbuf);
517
518 #ifdef __sparc
519     if ((len <= 0) && di_phdl)
520         len = di_prom_prop_lookup_ints(di_phdl, node, "reg", &regbuf);
521 #endif
522
523     if (len <= 0) {
524 #ifdef DEBUG
525         fprintf(stderr, "error = %x\n", errno);
526         fprintf(stderr, "can not find assigned-address\n");
527 #endif
528         return (DI_WALK_CONTINUE);
529     }
530
531     busno = PCI_REG_BUS_G(regbuf[0]);
532     devno = PCI_REG_DEV_G(regbuf[0]);
533     funcno = PCI_REG_FUNC_G(regbuf[0]);
534
535     if ((busno == devnode->bus) &&
536         (devno == devnode->dev) &&
537         (funcno == devnode->func)) {
538         devnode->node = node;
539
540         return (DI_WALK_TERMINATE);
541     }
542
543     return (DI_WALK_CONTINUE);
544 }
545
546 /*
547  * Solaris version
548  */
549 static int
550 pci_device_solx_devfs_probe( struct pci_device * dev )
551 {
552     int err = 0;
553     di_node_t rnode = DI_NODE_NIL;
554     i_devnode_t args = { 0, 0, 0, DI_NODE_NIL };
555     int *regbuf;
556     pci_regspec_t *reg;
557     int i;
558     int len = 0;
559     uint ent = 0;
560     struct pci_device_private *priv =
561         (struct pci_device_private *) dev;
562     nexus_t *nexus;
563
564     if ( (nexus = find_nexus_for_bus(dev->domain, dev->bus)) == NULL )
565         return ENODEV;
566
567     pci_device_cfg_read_u8(dev, &priv->header_type, PCI_CONF_HEADER);
568
569     pci_device_cfg_read_u8(dev, (uint8_t *)&dev->irq, PCI_CONF_ILINE);
570
571     /*
572      * starting to find if it is MEM/MEM64/IO
573      * using libdevinfo
574      */
575     if ((rnode = di_init(nexus->dev_path, DINFOCPYALL)) == DI_NODE_NIL) {
576         err = errno;
577         (void) fprintf(stderr, "di_init failed: %s\n", strerror(errno));
578     } else {
579         args.bus = dev->bus;
580         args.dev = dev->dev;
581         args.func = dev->func;
582         (void) di_walk_node(rnode, DI_WALK_CLDFIRST,
583                 (void *)&args, find_target_node);
584     }
585
586     if (args.node != DI_NODE_NIL) {
587         int *prop;
588 #ifdef __sparc
589         di_minor_t minor;
590 #endif
591
592         priv->is_primary = 0;
593
594 #ifdef __sparc
595         if (minor = di_minor_next(args.node, DI_MINOR_NIL))
596             MAPPING_DEV_PATH(dev) = di_devfs_minor_path (minor);
597         else
598             MAPPING_DEV_PATH(dev) = NULL;
599 #endif
600
601         if (di_prop_lookup_ints(DDI_DEV_T_ANY, args.node,
602                                 "primary-controller", &prop) >= 1) {
603             if (prop[0])
604                 priv->is_primary = 1;
605         }
606
607         /*
608          * It will succeed for sure, because it was
609          * successfully called in find_target_node
610          */
611         len = di_prop_lookup_ints(DDI_DEV_T_ANY, args.node,
612                                   "assigned-addresses",
613                                   &regbuf);
614
615 #ifdef __sparc
616         if ((len <= 0) && di_phdl) {
617             len = di_prom_prop_lookup_ints(di_phdl, args.node,
618                                 "assigned-addresses", &regbuf);
619         }
620 #endif
621     }
622
623     if (len <= 0)
624         goto cleanup;
625
626     /*
627      * Each BAR address get its own region slot in sequence.
628      * 32 bit BAR:
629      * BAR 0x10 -> slot0, BAR 0x14 -> slot1...
630      * 64 bit BAR:
631      * BAR 0x10 -> slot0, BAR 0x18 -> slot2...,
632      * slot1 is part of BAR 0x10
633      * Linux give two region slot for 64 bit address.
634      */
635     for (i = 0; i < len; i = i + CELL_NUMS_1275) {
636
637         reg = (pci_regspec_t *)&regbuf[i];
638         ent = reg->pci_phys_hi & 0xff;
639
640         if (ent > PCI_CONF_ROM) {
641             fprintf(stderr, "error ent = %d\n", ent);
642             break;
643         }
644         /*
645          * G35 broken in BAR0
646          */
647         if (ent < PCI_CONF_BASE0) {
648             /*
649              * VGA resource here and ignore it
650              */
651             break;
652         } else if (ent == PCI_CONF_ROM) {
653             priv->rom_base = reg->pci_phys_low |
654                 ((uint64_t)reg->pci_phys_mid << 32);
655             dev->rom_size = reg->pci_size_low;
656         } else {
657             ent = (ent - PCI_CONF_BASE0) >> 2;
658             /*
659              * non relocatable resource is excluded
660              * such like 0xa0000, 0x3b0. If it is met,
661              * the loop is broken;
662              */
663             if (!PCI_REG_REG_G(reg->pci_phys_hi))
664                 break;
665
666             if (reg->pci_phys_hi & PCI_PREFETCH_B) {
667                 dev->regions[ent].is_prefetchable = 1;
668             }
669
670
671             dev->regions[ent].base_addr = reg->pci_phys_low |
672                 ((uint64_t)reg->pci_phys_mid << 32);
673             dev->regions[ent].size = reg->pci_size_low |
674                 ((uint64_t)reg->pci_size_hi << 32);
675
676             switch (reg->pci_phys_hi & PCI_REG_ADDR_M) {
677                 case PCI_ADDR_IO:
678                     dev->regions[ent].is_IO = 1;
679                     break;
680                 case PCI_ADDR_MEM32:
681                     break;
682                 case PCI_ADDR_MEM64:
683                     dev->regions[ent].is_64 = 1;
684                     /*
685                      * Skip one slot for 64 bit address
686                      */
687                     break;
688             }
689         }
690     }
691
692   cleanup:
693     if (rnode != DI_NODE_NIL) {
694         di_fini(rnode);
695     }
696     return (err);
697 }
698
699 /**
700  * Map a memory region for a device using /dev/xsvc (x86) or fb device (sparc)
701  *
702  * \param dev   Device whose memory region is to be mapped.
703  * \param map   Parameters of the mapping that is to be created.
704  *
705  * \return
706  * Zero on success or an \c errno value on failure.
707  */
708 static int
709 pci_device_solx_devfs_map_range(struct pci_device *dev,
710                                 struct pci_device_mapping *map)
711 {
712     const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
713                         ? (PROT_READ | PROT_WRITE) : PROT_READ;
714     int err = 0;
715
716     const char *map_dev;
717     int         map_fd;
718
719 #ifdef __sparc
720     char        map_dev_buf[128];
721
722     if (MAPPING_DEV_PATH(dev)) {
723         snprintf(map_dev_buf, sizeof (map_dev_buf), "%s%s",
724                  "/devices", MAPPING_DEV_PATH(dev));
725         map_dev = map_dev_buf;
726     }
727     else
728         map_dev = "/dev/fb0";
729
730     map_fd = -1;
731 #else
732     /*
733      * Still uses xsvc to do the user space mapping on x86/x64,
734      * caches open fd across multiple calls.
735      */
736     map_dev = "/dev/xsvc";
737     map_fd = xsvc_fd;
738 #endif
739
740     if (map_fd < 0) {
741         if ((map_fd = open(map_dev, O_RDWR | O_CLOEXEC)) < 0) {
742             err = errno;
743             (void) fprintf(stderr, "can not open %s: %s\n", map_dev,
744                            strerror(errno));
745             return err;
746         }
747     }
748
749     map->memory = mmap(NULL, map->size, prot, MAP_SHARED, map_fd, map->base);
750     if (map->memory == MAP_FAILED) {
751         err = errno;
752
753         (void) fprintf(stderr, "map rom region =%llx failed: %s\n",
754                        (unsigned long long) map->base, strerror(errno));
755     }
756
757 #ifdef __sparc
758     close (map_fd);
759 #endif
760
761     return err;
762 }
763
764 /*
765  * Solaris version: read the VGA ROM data
766  */
767 static int
768 pci_device_solx_devfs_read_rom( struct pci_device * dev, void * buffer )
769 {
770     int err;
771     struct pci_device_mapping prom = {
772         .base = 0xC0000,
773         .size = 0x10000,
774         .flags = 0
775     };
776     struct pci_device_private *priv =
777         (struct pci_device_private *) dev;
778
779     if (priv->rom_base) {
780         prom.base = priv->rom_base;
781         prom.size = dev->rom_size;
782     }
783
784     err = pci_device_solx_devfs_map_range(dev, &prom);
785     if (err == 0) {
786         (void) bcopy(prom.memory, buffer, dev->rom_size);
787
788         if (munmap(prom.memory, prom.size) == -1) {
789             err = errno;
790         }
791     }
792     return err;
793 }
794
795 /*
796  * solaris version: Read the configurations space of the devices
797  */
798 static int
799 pci_device_solx_devfs_read( struct pci_device * dev, void * data,
800                              pciaddr_t offset, pciaddr_t size,
801                              pciaddr_t * bytes_read )
802 {
803     pcitool_reg_t cfg_prg;
804     int err = 0;
805     int i = 0;
806     nexus_t *nexus;
807
808     nexus = find_nexus_for_bus(dev->domain, dev->bus);
809
810     *bytes_read = 0;
811
812     if ( nexus == NULL ) {
813         return ENODEV;
814     }
815
816     cfg_prg.offset = offset;
817     cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_1 + NATIVE_ENDIAN;
818     cfg_prg.bus_no = dev->bus;
819     cfg_prg.dev_no = dev->dev;
820     cfg_prg.func_no = dev->func;
821     cfg_prg.barnum = 0;
822     cfg_prg.user_version = PCITOOL_USER_VERSION;
823
824     for (i = 0; i < size; i += PCITOOL_ACC_ATTR_SIZE(PCITOOL_ACC_ATTR_SIZE_1))
825     {
826         cfg_prg.offset = offset + i;
827
828         if ((err = ioctl(nexus->fd, PCITOOL_DEVICE_GET_REG, &cfg_prg)) != 0) {
829             fprintf(stderr, "read bdf<%s,%x,%x,%x,%llx> config space failure\n",
830                     nexus->path,
831                     cfg_prg.bus_no,
832                     cfg_prg.dev_no,
833                     cfg_prg.func_no,
834                     (unsigned long long) cfg_prg.offset);
835             fprintf(stderr, "Failure cause = %x\n", err);
836             break;
837         }
838
839         ((uint8_t *)data)[i] = (uint8_t)cfg_prg.data;
840         /*
841          * DWORDS Offset or bytes Offset ??
842          */
843     }
844     *bytes_read = i;
845
846     return (err);
847 }
848
849 /*
850  * Solaris version
851  */
852 static int
853 pci_device_solx_devfs_write( struct pci_device * dev, const void * data,
854                              pciaddr_t offset, pciaddr_t size,
855                              pciaddr_t * bytes_written )
856 {
857     pcitool_reg_t cfg_prg;
858     int err = 0;
859     int cmd;
860     nexus_t *nexus;
861
862     nexus = find_nexus_for_bus(dev->domain, dev->bus);
863
864     if ( bytes_written != NULL ) {
865         *bytes_written = 0;
866     }
867
868     if ( nexus == NULL ) {
869         return ENODEV;
870     }
871
872     cfg_prg.offset = offset;
873     switch (size) {
874         case 1:
875             cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_1 + NATIVE_ENDIAN;
876             cfg_prg.data = *((const uint8_t *)data);
877             break;
878         case 2:
879             cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_2 + NATIVE_ENDIAN;
880             cfg_prg.data = *((const uint16_t *)data);
881             break;
882         case 4:
883             cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_4 + NATIVE_ENDIAN;
884             cfg_prg.data = *((const uint32_t *)data);
885             break;
886         case 8:
887             cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_8 + NATIVE_ENDIAN;
888             cfg_prg.data = *((const uint64_t *)data);
889             break;
890         default:
891             return EINVAL;
892     }
893     cfg_prg.bus_no = dev->bus;
894     cfg_prg.dev_no = dev->dev;
895     cfg_prg.func_no = dev->func;
896     cfg_prg.barnum = 0;
897     cfg_prg.user_version = PCITOOL_USER_VERSION;
898
899     /*
900      * Check if this device is bridge device.
901      * If it is, it is also a nexus node???
902      * It seems that there is no explicit
903      * PCI nexus device for X86, so not applicable
904      * from pcitool_bus_reg_ops in pci_tools.c
905      */
906     cmd = PCITOOL_DEVICE_SET_REG;
907
908     if ((err = ioctl(nexus->fd, cmd, &cfg_prg)) != 0) {
909         return (err);
910     }
911     *bytes_written = size;
912
913     return (err);
914 }
915
916 static int pci_device_solx_devfs_boot_vga(struct pci_device *dev)
917 {
918     struct pci_device_private *priv =
919         (struct pci_device_private *) dev;
920
921     return (priv->is_primary);
922
923 }
924
925 static struct pci_io_handle *
926 pci_device_solx_devfs_open_legacy_io(struct pci_io_handle *ret,
927                                      struct pci_device *dev,
928                                      pciaddr_t base, pciaddr_t size)
929 {
930 #ifdef __x86
931     if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) == 0) {
932         ret->base = base;
933         ret->size = size;
934         return ret;
935     }
936 #endif
937     return NULL;
938 }
939
940 static uint32_t
941 pci_device_solx_devfs_read32(struct pci_io_handle *handle, uint32_t reg)
942 {
943 #ifdef __x86
944     uint16_t port = (uint16_t) (handle->base + reg);
945     uint32_t ret;
946     __asm__ __volatile__("inl %1,%0":"=a"(ret):"d"(port));
947     return ret;
948 #else
949     return *(uint32_t *)((uintptr_t)handle->memory + reg);
950 #endif
951 }
952
953 static uint16_t
954 pci_device_solx_devfs_read16(struct pci_io_handle *handle, uint32_t reg)
955 {
956 #ifdef __x86
957     uint16_t port = (uint16_t) (handle->base + reg);
958     uint16_t ret;
959     __asm__ __volatile__("inw %1,%0":"=a"(ret):"d"(port));
960     return ret;
961 #else
962     return *(uint16_t *)((uintptr_t)handle->memory + reg);
963 #endif
964 }
965
966 static uint8_t
967 pci_device_solx_devfs_read8(struct pci_io_handle *handle, uint32_t reg)
968 {
969 #ifdef __x86
970     uint16_t port = (uint16_t) (handle->base + reg);
971     uint8_t ret;
972     __asm__ __volatile__("inb %1,%0":"=a"(ret):"d"(port));
973     return ret;
974 #else
975     return *(uint8_t *)((uintptr_t)handle->memory + reg);
976 #endif
977 }
978
979 static void
980 pci_device_solx_devfs_write32(struct pci_io_handle *handle, uint32_t reg,
981     uint32_t data)
982 {
983 #ifdef __x86
984       uint16_t port = (uint16_t) (handle->base + reg);
985       __asm__ __volatile__("outl %0,%1"::"a"(data), "d"(port));
986 #else
987       *(uint16_t *)((uintptr_t)handle->memory + reg) = data;
988 #endif
989 }
990
991 static void
992 pci_device_solx_devfs_write16(struct pci_io_handle *handle, uint32_t reg,
993     uint16_t data)
994 {
995 #ifdef __x86
996       uint16_t port = (uint16_t) (handle->base + reg);
997       __asm__ __volatile__("outw %0,%1"::"a"(data), "d"(port));
998 #else
999     *(uint8_t *)((uintptr_t)handle->memory + reg) = data;
1000 #endif
1001 }
1002
1003 static void
1004 pci_device_solx_devfs_write8(struct pci_io_handle *handle, uint32_t reg,
1005     uint8_t data)
1006 {
1007 #ifdef __x86
1008       uint16_t port = (uint16_t) (handle->base + reg);
1009       __asm__ __volatile__("outb %0,%1"::"a"(data), "d"(port));
1010 #else
1011       *(uint32_t *)((uintptr_t)handle->memory + reg) = data;
1012 #endif
1013 }
1014
1015 static int
1016 pci_device_solx_devfs_map_legacy(struct pci_device *dev, pciaddr_t base,
1017                                  pciaddr_t size, unsigned map_flags,
1018                                  void **addr)
1019 {
1020     int err;
1021     struct pci_device_mapping map = {
1022         .base = base,
1023         .size = size,
1024         .flags = map_flags,
1025     };
1026
1027     err = pci_device_solx_devfs_map_range(dev, &map);
1028     if (err == 0)
1029         *addr = map.memory;
1030     return err;
1031 }
1032
1033 static int
1034 pci_device_solx_devfs_unmap_legacy(struct pci_device *dev,
1035                                    void *addr, pciaddr_t size)
1036 {
1037     struct pci_device_mapping map = {
1038         .memory = addr,
1039         .size = size,
1040     };
1041
1042     return pci_device_generic_unmap_range(dev, &map);
1043 }
1044
1045 static const struct pci_system_methods solx_devfs_methods = {
1046     .destroy = pci_system_solx_devfs_destroy,
1047 #ifdef __sparc
1048     .destroy_device = pci_system_solx_devfs_destroy_device,
1049 #else
1050     .destroy_device = NULL,
1051 #endif
1052     .read_rom = pci_device_solx_devfs_read_rom,
1053     .probe = pci_device_solx_devfs_probe,
1054     .map_range = pci_device_solx_devfs_map_range,
1055     .unmap_range = pci_device_generic_unmap_range,
1056
1057     .read = pci_device_solx_devfs_read,
1058     .write = pci_device_solx_devfs_write,
1059
1060     .fill_capabilities = pci_fill_capabilities_generic,
1061     .boot_vga = pci_device_solx_devfs_boot_vga,
1062
1063     .open_legacy_io = pci_device_solx_devfs_open_legacy_io,
1064     .read32 = pci_device_solx_devfs_read32,
1065     .read16 = pci_device_solx_devfs_read16,
1066     .read8 = pci_device_solx_devfs_read8,
1067     .write32 = pci_device_solx_devfs_write32,
1068     .write16 = pci_device_solx_devfs_write16,
1069     .write8 = pci_device_solx_devfs_write8,
1070     .map_legacy = pci_device_solx_devfs_map_legacy,
1071     .unmap_legacy = pci_device_solx_devfs_unmap_legacy,
1072 };
1073
1074 /*
1075  * Attempt to access PCI subsystem using Solaris's devfs interface.
1076  * Solaris version
1077  */
1078 _pci_hidden int
1079 pci_system_solx_devfs_create( void )
1080 {
1081     int err = 0;
1082     di_node_t di_node;
1083     probe_info_t pinfo;
1084     struct pci_device_private *devices;
1085
1086     if (nexus_list != NULL) {
1087         return 0;
1088     }
1089
1090     if ((di_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
1091         err = errno;
1092         (void) fprintf(stderr, "di_init() failed: %s\n",
1093                        strerror(errno));
1094         return (err);
1095     }
1096
1097     if ((devices = calloc(INITIAL_NUM_DEVICES,
1098                         sizeof (struct pci_device_private))) == NULL) {
1099         err = errno;
1100         di_fini(di_node);
1101         return (err);
1102     }
1103
1104 #ifdef __sparc
1105     if ((di_phdl = di_prom_init()) == DI_PROM_HANDLE_NIL)
1106         (void) fprintf(stderr, "di_prom_init failed: %s\n", strerror(errno));
1107 #endif
1108
1109     pinfo.num_allocated_elems = INITIAL_NUM_DEVICES;
1110     pinfo.num_devices = 0;
1111     pinfo.devices = devices;
1112 #ifdef __sparc
1113     nexus_count = 0;
1114 #endif
1115     (void) di_walk_minor(di_node, DDI_NT_REGACC, 0, &pinfo, probe_nexus_node);
1116
1117     di_fini(di_node);
1118
1119     if ((pci_sys = calloc(1, sizeof (struct pci_system))) == NULL) {
1120         err = errno;
1121         free(devices);
1122         return (err);
1123     }
1124
1125     pci_sys->methods = &solx_devfs_methods;
1126     pci_sys->devices = pinfo.devices;
1127     pci_sys->num_devices = pinfo.num_devices;
1128
1129     return (err);
1130 }