2 .\" Copyright (c) 2014, 2015 Marcel Moolenaar
3 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
15 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 .Nd Generic prototyping and diagnostics driver
37 To compile this driver into the kernel,
38 place the following line in your
39 kernel configuration file:
40 .Bd -ragged -offset indent
44 Alternatively, to load the driver as a
45 module at boot time, place the following line in
47 .Bd -literal -offset indent
51 To have the driver attach to a device instead of its regular driver,
52 mention it in the list of devices assigned to the following loader variable:
53 .Bd -ragged -offset indent
54 hw.proto.attach="desc[,desc]"
60 device driver attaches to PCI or ISA devices when no other device drivers
61 are present for those devices and it creates device special files for all
62 resources associated with the device.
63 The driver itself has no knowledge of the device it attaches to.
64 Programs can open these device special files and perform register-level
68 device driver is nothing but a conduit or gateway between user space
69 programs and the hardware device.
71 Examples for why this is useful include hardware diagnostics and prototyping.
72 In both these use cases, it is far more convenient to develop and run the
74 Especially hardware diagnostics requires a somewhat user-friendly interface
75 and adequate reporting.
76 Neither is done easily as kernel code.
77 .Ss I/O port resources
78 Device special files created for I/O port resources allow
84 operations to be performed on them.
89 system calls are used to perform input and output (resp.) on the port.
90 The amount of data that can be read or written at any single time is either
94 driver does not prevent reading or writing 8 bytes at a time for some
95 architectures, it should not be assumed that such actually produces
99 system call is used to select the port number, relative to the I/O port
100 region being represented by the device special file.
101 If, for example, the device special file corresponds to an I/O port region
102 from 0x3f8 to 0x3ff inclusive, then an offset of 4 given to lseek with a
103 whence value of SEEK_SET will target port 0x3fc on the next read or write
107 system call can be used for the
110 This ioctl request returns the extend of the resource covered by this
111 device special file. The extend is returned in the following structure:
113 struct proto_ioc_region {
114 unsigned long address;
118 .Ss Memory mapped I/O resources
119 The device special files created for memory mapped I/O resources behave
120 in the same way as those created for I/O port resources.
121 Additionally, device special files for memory mapped I/O resources allow
122 the memory to be mapped into the process' address space using
124 Reads and writes to the memory address returned by
126 go directly to the hardware.
131 can be avoided, reducing the access overhead significantly.
132 Alignment and access width constraints put forth by the underlying device
134 Also, make sure the compiler does not optimize memory accesses away or has
135 them coalesced into bigger accesses.
136 .Ss DMA pseudo resource
137 A device special file named
139 is created for the purpose of doing DMA.
145 This device special file does not support
151 request has an argument that is both in and out and is defined as
154 struct proto_ioc_busdma {
155 unsigned int request;
161 unsigned long maxaddr;
163 unsigned long maxsegsz;
165 unsigned int datarate;
171 unsigned long virt_addr;
172 unsigned long virt_size;
173 unsigned int phys_nsegs;
174 unsigned long phys_addr;
175 unsigned long bus_addr;
176 unsigned int bus_nsegs;
184 unsigned long result;
189 field is used to specify which DMA operation is to be performed.
192 field is used to specify which object the operation applies to.
193 An object is either a tag or a memory descriptor (md).
194 The following DMA operations are defined:
196 .It PROTO_IOC_BUSDMA_TAG_CREATE
200 field is set on output with the key of the DMA tag.
201 The tag is created with the constraints given by the
203 sub-structure. These constraints correspond roughly to those that can be
205 .Xr bus_dma_tag_create 9
207 .It PROTO_IOC_BUSDMA_TAG_DERIVE
208 Create a derived tag.
211 field is used to identify the parent tag from which to derive the new tag.
212 The key of the derived tag is returned in the
215 The derived tag combines the constraints of the parent tag with those
219 The combined constraints are written back to the
221 sub-structure on return.
222 .It PROTO_IOC_BUSDMA_TAG_DESTROY
223 Destroy a root or derived tag previously created.
226 field specifies the tag to destroy.
227 A tag can only be destroyed when not referenced anymore.
228 This means that derived tags that have this tag as a parent and memory
229 descriptors created from this tag must be destroyed first.
230 .It PROTO_IOC_BUSDMA_MEM_ALLOC
231 Allocate memory that satisfies the constraints put forth by the tag
237 The key of the memory descriptor for this memory is returned in the
242 sub-structure is filled on return with details of the allocation.
243 The kernel virtual address and the size of the allocated memory are returned
249 The number of contigous physical memory segments and the address of the first
250 segment are returned in the
255 Allocated memory is automatically loaded and thus mapped into bus space.
256 The number of bus segments and the address of the first segment are returned
262 The behaviour of this operation banks heavily on how
263 .Xr bus_dmamem_alloc 9
264 is implemented, which means that memory is currently always allocated as a
265 single contigous region of physical memory.
266 In practice this also tends to give a single contigous region in bus space.
267 This may change over time.
268 .It PROTO_IOC_BUSDMA_MEM_FREE
269 Free previously allocated memory and destroy the memory desciptor.
272 driver is not in a position to track whether the memory has been mapped in
273 the process' address space, so the application is responsible for unmapping
274 the memory before it is freed.
277 driver also cannot protect against the hardware writing to or reading from
278 the memory, even after it has been freed.
279 When the memory is reused for other purposes it can be corrupted or cause
280 the hardware to behave in unpredictable ways when DMA has not stopped
281 completely before freeing.
282 .It PROTO_IOC_BUSDMA_MD_CREATE
283 Create an empty memory descriptor with the tag specified in the
288 The key of the memory descriptor is returned in the
291 .It PROTO_IOC_BUSDMA_MD_DESTROY
292 Destroy the previously created memory descriptor specified by the
295 When the memory descriptor is still loaded, it is unloaded first.
296 .It PROTO_IOC_BUSDMA_MD_LOAD
297 Load a contigous region of memory in the memory descriptor specified by the
300 The size and address in the process' virtual address space are specified
308 sub-structure contains the result of the operation.
309 The number of physical segments and the address of the first segment is
315 The number of bus space segments and the address of the first segment in
316 bus space is returned in the
321 .It PROTO_IOC_BUSDMA_MD_UNLOAD
322 Unload the memory descriptor specified by the
325 .It PROTO_IOC_BUSDMA_SYNC
326 Guarantee that all hardware components have a coherent view of the memory
327 tracked by the memory descriptor, specified by the
330 A sub-section of the memory can be targeted by specifying the relative
331 offset and size of the memory to make coherent.
332 The offset and size are given by the
341 field holds the sync operation to be performed.
342 This is similar to the
343 .Xr bus_dmamap_sync 9
346 .Ss PCI configuration space
347 Access to PCI configuration space is possible through the
350 The device special file supports
355 Usage is the asme as for I/O port resources.
357 All device special files corresponding to a PCI device are located under
358 .Pa /dev/proto/pci<d>:<b>:<s>:<f>
360 .Pa pci<d>:<b>:<s>:<f>
361 representing the location of the PCI device in the PCI hierarchy.
362 A PCI location includes:
364 .Bl -tag -width XXXXXX -compact -offset indent
366 The PCI domain number
370 The PCI slot or device number
372 The PCI function number
375 Every PCI device has a device special file called
377 This device special file gives access to the PCI configuration space.
378 A device special file called
381 This device special file provides the interfaces needed for doing DMA.
382 For each valid base address register (BAR), a device special file is created
383 that contains the BAR offset and the resource type.
384 A resource type can be either
388 representing I/O port or memory mapped I/O space (resp.)
390 ISA devices do not have a location. Instead, they are identified by the
391 first I/O port address or first memory mapped I/O address.
392 Consequently, all device special files corresponding to an ISA device are
394 .Pa /dev/proto/isa:<addr>
397 the address in hexadecimal notation.
398 For each I/O port or memory mapped I/O address, a device special file is
399 created that contains the resource identification used by the kernel and
401 The resource type can be either
405 representing I/O port or memory mapped I/O space (resp.)
406 When the device has a DMA channel assigned to it, a device special file
410 This device special file provides the interfaces needed for doing DMA.
412 If the ISA device is not a Plug-and-Play device nor present in the ACPI
413 device tree, it must have the appropriate hints so that the kernel can
414 reserve the resources for it.
417 A single function PCI device in domain 0, on bus 1, in slot 2 and having a
418 single memory mapped I/O region will have the following device special files:
420 .Bl -tag -width XXXXXX -compact -offset indent
421 .It Pa /dev/proto/pci0:1:2:0/10.mem
422 .It Pa /dev/proto/pci0:1:2:0/pcicfg
425 A legacy floppy controller will have the following device files:
427 .Bl -tag -width XXXXXX -compact -offset indent
428 .It Pa /dev/proto/isa:0x3f0/00.io
429 .It Pa /dev/proto/isa:0x3f0/01.io
430 .It Pa /dev/proto/isa:0x3f0/busdma
439 .Xr bus_dma_tag_create 9 ,
440 .Xr bus_dmamap_sync 9 ,
441 .Xr bus_dmamem_alloc 9
446 device driver and this manual page were written by
447 .An Marcel Moolenaar Aq Mt marcel@xcllnt.net .
448 .Sh SECURITY CONSIDERATIONS
449 Because programs have direct access to the hardware, the
451 driver is inherently insecure.
452 It is not advisable to use this driver on a production machine.
454 .Sh MISSING FUNCTIONALITY
457 driver does not fully support memory descriptors that need multiple
458 physical memory segments or multiple bus space segments.
459 At the very least, an operation is needed on the DMA pseudo resource
460 for the application to obtain all segments.
464 driver does not yet support interrupts.
465 Since interrupts cannot be handled by the driver itself, they must be
466 converted into signals and delivered to the program that has registered
468 A satisfactory mechanism for keeping the interrupt masked during the
469 signal handling is still being worked out.
471 DMA support for devices other than busmaster devices is not present yet.
472 The details of how a program is to interact with the DMA controller still
473 need to be fleshed out.