xref: /freebsd/sys/kern/bus_if.m (revision a812392203d7c4c3f0db9d8a0f3391374c49c71f)
1#-
2# Copyright (c) 1998-2004 Doug Rabson
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26# $FreeBSD$
27#
28
29#include <sys/types.h>
30#include <sys/systm.h>
31#include <sys/bus.h>
32
33/**
34 * @defgroup BUS bus - KObj methods for drivers of devices with children
35 * @brief A set of methods required device drivers that support
36 * child devices.
37 * @{
38 */
39INTERFACE bus;
40
41#
42# Default implementations of some methods.
43#
44CODE {
45	static struct resource *
46	null_alloc_resource(device_t dev, device_t child,
47	    int type, int *rid, u_long start, u_long end,
48	    u_long count, u_int flags)
49	{
50	    return (0);
51	}
52
53	static int
54	null_remap_intr(device_t bus, device_t dev, u_int irq)
55	{
56
57		if (dev != NULL)
58			return (BUS_REMAP_INTR(dev, NULL, irq));
59		return (ENXIO);
60	}
61
62	static device_t
63	null_add_child(device_t bus, int order, const char *name,
64	    int unit)
65	{
66
67		panic("bus_add_child is not implemented");
68	}
69};
70
71/**
72 * @brief Print a description of a child device
73 *
74 * This is called from system code which prints out a description of a
75 * device. It should describe the attachment that the child has with
76 * the parent. For instance the TurboLaser bus prints which node the
77 * device is attached to. See bus_generic_print_child() for more
78 * information.
79 *
80 * @param _dev		the device whose child is being printed
81 * @param _child	the child device to describe
82 *
83 * @returns		the number of characters output.
84 */
85METHOD int print_child {
86	device_t _dev;
87	device_t _child;
88} DEFAULT bus_generic_print_child;
89
90/**
91 * @brief Print a notification about an unprobed child device.
92 *
93 * Called for each child device that did not succeed in probing for a
94 * driver.
95 *
96 * @param _dev		the device whose child was being probed
97 * @param _child	the child device which failed to probe
98 */
99METHOD void probe_nomatch {
100        device_t _dev;
101        device_t _child;
102};
103
104/**
105 * @brief Read the value of a bus-specific attribute of a device
106 *
107 * This method, along with BUS_WRITE_IVAR() manages a bus-specific set
108 * of instance variables of a child device.  The intention is that
109 * each different type of bus defines a set of appropriate instance
110 * variables (such as ports and irqs for ISA bus etc.)
111 *
112 * This information could be given to the child device as a struct but
113 * that makes it hard for a bus to add or remove variables without
114 * forcing an edit and recompile for all drivers which may not be
115 * possible for vendor supplied binary drivers.
116 *
117 * This method copies the value of an instance variable to the
118 * location specified by @p *_result.
119 *
120 * @param _dev		the device whose child was being examined
121 * @param _child	the child device whose instance variable is
122 *			being read
123 * @param _index	the instance variable to read
124 * @param _result	a loction to recieve the instance variable
125 *			value
126 *
127 * @retval 0		success
128 * @retval ENOENT	no such instance variable is supported by @p
129 *			_dev
130 */
131METHOD int read_ivar {
132	device_t _dev;
133	device_t _child;
134	int _index;
135	uintptr_t *_result;
136};
137
138/**
139 * @brief Write the value of a bus-specific attribute of a device
140 *
141 * This method sets the value of an instance variable to @p _value.
142 *
143 * @param _dev		the device whose child was being updated
144 * @param _child	the child device whose instance variable is
145 *			being written
146 * @param _index	the instance variable to write
147 * @param _value	the value to write to that instance variable
148 *
149 * @retval 0		success
150 * @retval ENOENT	no such instance variable is supported by @p
151 *			_dev
152 * @retval EINVAL	the instance variable was recognised but
153 *			contains a read-only value
154 */
155METHOD int write_ivar {
156	device_t _dev;
157	device_t _child;
158	int _indx;
159	uintptr_t _value;
160};
161
162/**
163 * @brief Notify a bus that a child was deleted
164 *
165 * Called at the beginning of device_delete_child() to allow the parent
166 * to teardown any bus-specific state for the child.
167 *
168 * @param _dev		the device whose child is being deleted
169 * @param _child	the child device which is being deleted
170 */
171METHOD void child_deleted {
172	device_t _dev;
173	device_t _child;
174};
175
176/**
177 * @brief Notify a bus that a child was detached
178 *
179 * Called after the child's DEVICE_DETACH() method to allow the parent
180 * to reclaim any resources allocated on behalf of the child.
181 *
182 * @param _dev		the device whose child changed state
183 * @param _child	the child device which changed state
184 */
185METHOD void child_detached {
186	device_t _dev;
187	device_t _child;
188};
189
190/**
191 * @brief Notify a bus that a new driver was added
192 *
193 * Called when a new driver is added to the devclass which owns this
194 * bus. The generic implementation of this method attempts to probe and
195 * attach any un-matched children of the bus.
196 *
197 * @param _dev		the device whose devclass had a new driver
198 *			added to it
199 * @param _driver	the new driver which was added
200 */
201METHOD void driver_added {
202	device_t _dev;
203	driver_t *_driver;
204} DEFAULT bus_generic_driver_added;
205
206/**
207 * @brief Create a new child device
208 *
209 * For busses which use use drivers supporting DEVICE_IDENTIFY() to
210 * enumerate their devices, this method is used to create new
211 * device instances. The new device will be added after the last
212 * existing child with the same order.
213 *
214 * @param _dev		the bus device which will be the parent of the
215 *			new child device
216 * @param _order	a value which is used to partially sort the
217 *			children of @p _dev - devices created using
218 *			lower values of @p _order appear first in @p
219 *			_dev's list of children
220 * @param _name		devclass name for new device or @c NULL if not
221 *			specified
222 * @param _unit		unit number for new device or @c -1 if not
223 *			specified
224 */
225METHOD device_t add_child {
226	device_t _dev;
227	u_int _order;
228	const char *_name;
229	int _unit;
230} DEFAULT null_add_child;
231
232/**
233 * @brief Allocate a system resource
234 *
235 * This method is called by child devices of a bus to allocate resources.
236 * The types are defined in <machine/resource.h>; the meaning of the
237 * resource-ID field varies from bus to bus (but @p *rid == 0 is always
238 * valid if the resource type is). If a resource was allocated and the
239 * caller did not use the RF_ACTIVE to specify that it should be
240 * activated immediately, the caller is responsible for calling
241 * BUS_ACTIVATE_RESOURCE() when it actually uses the resource.
242 *
243 * @param _dev		the parent device of @p _child
244 * @param _child	the device which is requesting an allocation
245 * @param _type		the type of resource to allocate
246 * @param _rid		a pointer to the resource identifier
247 * @param _start	hint at the start of the resource range - pass
248 *			@c 0UL for any start address
249 * @param _end		hint at the end of the resource range - pass
250 *			@c ~0UL for any end address
251 * @param _count	hint at the size of range required - pass @c 1
252 *			for any size
253 * @param _flags	any extra flags to control the resource
254 *			allocation - see @c RF_XXX flags in
255 *			<sys/rman.h> for details
256 *
257 * @returns		the resource which was allocated or @c NULL if no
258 *			resource could be allocated
259 */
260METHOD struct resource * alloc_resource {
261	device_t	_dev;
262	device_t	_child;
263	int		_type;
264	int	       *_rid;
265	u_long		_start;
266	u_long		_end;
267	u_long		_count;
268	u_int		_flags;
269} DEFAULT null_alloc_resource;
270
271/**
272 * @brief Activate a resource
273 *
274 * Activate a resource previously allocated with
275 * BUS_ALLOC_RESOURCE(). This may for instance map a memory region
276 * into the kernel's virtual address space.
277 *
278 * @param _dev		the parent device of @p _child
279 * @param _child	the device which allocated the resource
280 * @param _type		the type of resource
281 * @param _rid		the resource identifier
282 * @param _r		the resource to activate
283 */
284METHOD int activate_resource {
285	device_t	_dev;
286	device_t	_child;
287	int		_type;
288	int		_rid;
289	struct resource *_r;
290};
291
292/**
293 * @brief Deactivate a resource
294 *
295 * Deactivate a resource previously allocated with
296 * BUS_ALLOC_RESOURCE(). This may for instance unmap a memory region
297 * from the kernel's virtual address space.
298 *
299 * @param _dev		the parent device of @p _child
300 * @param _child	the device which allocated the resource
301 * @param _type		the type of resource
302 * @param _rid		the resource identifier
303 * @param _r		the resource to deactivate
304 */
305METHOD int deactivate_resource {
306	device_t	_dev;
307	device_t	_child;
308	int		_type;
309	int		_rid;
310	struct resource *_r;
311};
312
313/**
314 * @brief Adjust a resource
315 *
316 * Adjust the start and/or end of a resource allocated by
317 * BUS_ALLOC_RESOURCE.  At least part of the new address range must overlap
318 * with the existing address range.  If the successful, the resource's range
319 * will be adjusted to [start, end] on return.
320 *
321 * @param _dev		the parent device of @p _child
322 * @param _child	the device which allocated the resource
323 * @param _type		the type of resource
324 * @param _res		the resource to adjust
325 * @param _start	the new starting address of the resource range
326 * @param _end		the new ending address of the resource range
327 */
328METHOD int adjust_resource {
329	device_t	_dev;
330	device_t	_child;
331	int		_type;
332	struct resource *_res;
333	u_long		_start;
334	u_long		_end;
335};
336
337/**
338 * @brief Release a resource
339 *
340 * Free a resource allocated by the BUS_ALLOC_RESOURCE.  The @p _rid
341 * value must be the same as the one returned by BUS_ALLOC_RESOURCE()
342 * (which is not necessarily the same as the one the client passed).
343 *
344 * @param _dev		the parent device of @p _child
345 * @param _child	the device which allocated the resource
346 * @param _type		the type of resource
347 * @param _rid		the resource identifier
348 * @param _r		the resource to release
349 */
350METHOD int release_resource {
351	device_t	_dev;
352	device_t	_child;
353	int		_type;
354	int		_rid;
355	struct resource *_res;
356};
357
358/**
359 * @brief Install an interrupt handler
360 *
361 * This method is used to associate an interrupt handler function with
362 * an irq resource. When the interrupt triggers, the function @p _intr
363 * will be called with the value of @p _arg as its single
364 * argument. The value returned in @p *_cookiep is used to cancel the
365 * interrupt handler - the caller should save this value to use in a
366 * future call to BUS_TEARDOWN_INTR().
367 *
368 * @param _dev		the parent device of @p _child
369 * @param _child	the device which allocated the resource
370 * @param _irq		the resource representing the interrupt
371 * @param _flags	a set of bits from enum intr_type specifying
372 *			the class of interrupt
373 * @param _intr		the function to call when the interrupt
374 *			triggers
375 * @param _arg		a value to use as the single argument in calls
376 *			to @p _intr
377 * @param _cookiep	a pointer to a location to recieve a cookie
378 *			value that may be used to remove the interrupt
379 *			handler
380 */
381METHOD int setup_intr {
382	device_t	_dev;
383	device_t	_child;
384	struct resource *_irq;
385	int		_flags;
386	driver_filter_t	*_filter;
387	driver_intr_t	*_intr;
388	void		*_arg;
389	void		**_cookiep;
390};
391
392/**
393 * @brief Uninstall an interrupt handler
394 *
395 * This method is used to disassociate an interrupt handler function
396 * with an irq resource. The value of @p _cookie must be the value
397 * returned from a previous call to BUS_SETUP_INTR().
398 *
399 * @param _dev		the parent device of @p _child
400 * @param _child	the device which allocated the resource
401 * @param _irq		the resource representing the interrupt
402 * @param _cookie	the cookie value returned when the interrupt
403 *			was originally registered
404 */
405METHOD int teardown_intr {
406	device_t	_dev;
407	device_t	_child;
408	struct resource	*_irq;
409	void		*_cookie;
410};
411
412/**
413 * @brief Define a resource which can be allocated with
414 * BUS_ALLOC_RESOURCE().
415 *
416 * This method is used by some busses (typically ISA) to allow a
417 * driver to describe a resource range that it would like to
418 * allocate. The resource defined by @p _type and @p _rid is defined
419 * to start at @p _start and to include @p _count indices in its
420 * range.
421 *
422 * @param _dev		the parent device of @p _child
423 * @param _child	the device which owns the resource
424 * @param _type		the type of resource
425 * @param _rid		the resource identifier
426 * @param _start	the start of the resource range
427 * @param _count	the size of the resource range
428 */
429METHOD int set_resource {
430	device_t	_dev;
431	device_t	_child;
432	int		_type;
433	int		_rid;
434	u_long		_start;
435	u_long		_count;
436};
437
438/**
439 * @brief Describe a resource
440 *
441 * This method allows a driver to examine the range used for a given
442 * resource without actually allocating it.
443 *
444 * @param _dev		the parent device of @p _child
445 * @param _child	the device which owns the resource
446 * @param _type		the type of resource
447 * @param _rid		the resource identifier
448 * @param _start	the address of a location to recieve the start
449 *			index of the resource range
450 * @param _count	the address of a location to recieve the size
451 *			of the resource range
452 */
453METHOD int get_resource {
454	device_t	_dev;
455	device_t	_child;
456	int		_type;
457	int		_rid;
458	u_long		*_startp;
459	u_long		*_countp;
460};
461
462/**
463 * @brief Delete a resource.
464 *
465 * Use this to delete a resource (possibly one previously added with
466 * BUS_SET_RESOURCE()).
467 *
468 * @param _dev		the parent device of @p _child
469 * @param _child	the device which owns the resource
470 * @param _type		the type of resource
471 * @param _rid		the resource identifier
472 */
473METHOD void delete_resource {
474	device_t	_dev;
475	device_t	_child;
476	int		_type;
477	int		_rid;
478};
479
480/**
481 * @brief Return a struct resource_list.
482 *
483 * Used by drivers which use bus_generic_rl_alloc_resource() etc. to
484 * implement their resource handling. It should return the resource
485 * list of the given child device.
486 *
487 * @param _dev		the parent device of @p _child
488 * @param _child	the device which owns the resource list
489 */
490METHOD struct resource_list * get_resource_list {
491	device_t	_dev;
492	device_t	_child;
493} DEFAULT bus_generic_get_resource_list;
494
495/**
496 * @brief Is the hardware described by @p _child still attached to the
497 * system?
498 *
499 * This method should return 0 if the device is not present.  It
500 * should return -1 if it is present.  Any errors in determining
501 * should be returned as a normal errno value.  Client drivers are to
502 * assume that the device is present, even if there is an error
503 * determining if it is there.  Busses are to try to avoid returning
504 * errors, but newcard will return an error if the device fails to
505 * implement this method.
506 *
507 * @param _dev		the parent device of @p _child
508 * @param _child	the device which is being examined
509 */
510METHOD int child_present {
511	device_t	_dev;
512	device_t	_child;
513} DEFAULT bus_generic_child_present;
514
515/**
516 * @brief Returns the pnp info for this device.
517 *
518 * Return it as a string.  If the string is insufficient for the
519 * storage, then return EOVERFLOW.
520 *
521 * @param _dev		the parent device of @p _child
522 * @param _child	the device which is being examined
523 * @param _buf		the address of a buffer to receive the pnp
524 *			string
525 * @param _buflen	the size of the buffer pointed to by @p _buf
526 */
527METHOD int child_pnpinfo_str {
528	device_t	_dev;
529	device_t	_child;
530	char		*_buf;
531	size_t		_buflen;
532};
533
534/**
535 * @brief Returns the location for this device.
536 *
537 * Return it as a string.  If the string is insufficient for the
538 * storage, then return EOVERFLOW.
539 *
540 * @param _dev		the parent device of @p _child
541 * @param _child	the device which is being examined
542 * @param _buf		the address of a buffer to receive the location
543 *			string
544 * @param _buflen	the size of the buffer pointed to by @p _buf
545 */
546METHOD int child_location_str {
547	device_t	_dev;
548	device_t	_child;
549	char		*_buf;
550	size_t		_buflen;
551};
552
553/**
554 * @brief Allow drivers to request that an interrupt be bound to a specific
555 * CPU.
556 *
557 * @param _dev		the parent device of @p _child
558 * @param _child	the device which allocated the resource
559 * @param _irq		the resource representing the interrupt
560 * @param _cpu		the CPU to bind the interrupt to
561 */
562METHOD int bind_intr {
563	device_t	_dev;
564	device_t	_child;
565	struct resource *_irq;
566	int		_cpu;
567} DEFAULT bus_generic_bind_intr;
568
569/**
570 * @brief Allow (bus) drivers to specify the trigger mode and polarity
571 * of the specified interrupt.
572 *
573 * @param _dev		the bus device
574 * @param _irq		the interrupt number to modify
575 * @param _trig		the trigger mode required
576 * @param _pol		the interrupt polarity required
577 */
578METHOD int config_intr {
579	device_t	_dev;
580	int		_irq;
581	enum intr_trigger _trig;
582	enum intr_polarity _pol;
583} DEFAULT bus_generic_config_intr;
584
585/**
586 * @brief Allow drivers to associate a description with an active
587 * interrupt handler.
588 *
589 * @param _dev		the parent device of @p _child
590 * @param _child	the device which allocated the resource
591 * @param _irq		the resource representing the interrupt
592 * @param _cookie	the cookie value returned when the interrupt
593 *			was originally registered
594 * @param _descr	the description to associate with the interrupt
595 */
596METHOD int describe_intr {
597	device_t	_dev;
598	device_t	_child;
599	struct resource *_irq;
600	void		*_cookie;
601	const char	*_descr;
602} DEFAULT bus_generic_describe_intr;
603
604/**
605 * @brief Notify a (bus) driver about a child that the hints mechanism
606 * believes it has discovered.
607 *
608 * The bus is responsible for then adding the child in the right order
609 * and discovering other things about the child.  The bus driver is
610 * free to ignore this hint, to do special things, etc.  It is all up
611 * to the bus driver to interpret.
612 *
613 * This method is only called in response to the parent bus asking for
614 * hinted devices to be enumerated.
615 *
616 * @param _dev		the bus device
617 * @param _dname	the name of the device w/o unit numbers
618 * @param _dunit	the unit number of the device
619 */
620METHOD void hinted_child {
621	device_t	_dev;
622	const char	*_dname;
623	int		_dunit;
624};
625
626/**
627 * @brief Returns bus_dma_tag_t for use w/ devices on the bus.
628 *
629 * @param _dev		the parent device of @p _child
630 * @param _child	the device to which the tag will belong
631 */
632METHOD bus_dma_tag_t get_dma_tag {
633	device_t	_dev;
634	device_t	_child;
635} DEFAULT bus_generic_get_dma_tag;
636
637/**
638 * @brief Allow the bus to determine the unit number of a device.
639 *
640 * @param _dev		the parent device of @p _child
641 * @param _child	the device whose unit is to be wired
642 * @param _name		the name of the device's new devclass
643 * @param _unitp	a pointer to the device's new unit value
644 */
645METHOD void hint_device_unit {
646	device_t	_dev;
647	device_t	_child;
648	const char	*_name;
649	int		*_unitp;
650};
651
652/**
653 * @brief Notify a bus that the bus pass level has been changed
654 *
655 * @param _dev		the bus device
656 */
657METHOD void new_pass {
658	device_t	_dev;
659} DEFAULT bus_generic_new_pass;
660
661/**
662 * @brief Notify a bus that specified child's IRQ should be remapped.
663 *
664 * @param _dev		the bus device
665 * @param _child	the child device
666 * @param _irq		the irq number
667 */
668METHOD int remap_intr {
669	device_t	_dev;
670	device_t	_child;
671	u_int		_irq;
672} DEFAULT null_remap_intr;
673
674/**
675 * @brief Suspend a given child
676 *
677 * @param _dev		the parent device of @p _child
678 * @param _child	the device to suspend
679 */
680METHOD int suspend_child {
681	device_t	_dev;
682	device_t	_child;
683} DEFAULT bus_generic_suspend_child;
684
685/**
686 * @brief Resume a given child
687 *
688 * @param _dev		the parent device of @p _child
689 * @param _child	the device to resume
690 */
691METHOD int resume_child {
692	device_t	_dev;
693	device_t	_child;
694} DEFAULT bus_generic_resume_child;
695
696/**
697 * @brief Get the VM domain handle for the given bus and child.
698 *
699 * @param _dev		the bus device
700 * @param _child	the child device
701 * @param _domain	a pointer to the bus's domain handle identifier
702 */
703METHOD int get_domain {
704	device_t	_dev;
705	device_t	_child;
706	int		*_domain;
707} DEFAULT bus_generic_get_domain;
708