bus_if.m revision 295755
156160Sru#-
2146515Sru# Copyright (c) 1998-2004 Doug Rabson
356160Sru# All rights reserved.
4146515Sru#
556160Sru# Redistribution and use in source and binary forms, with or without
656160Sru# modification, are permitted provided that the following conditions
756160Sru# are met:
856160Sru# 1. Redistributions of source code must retain the above copyright
956160Sru#    notice, this list of conditions and the following disclaimer.
1056160Sru# 2. Redistributions in binary form must reproduce the above copyright
1156160Sru#    notice, this list of conditions and the following disclaimer in the
1256160Sru#    documentation and/or other materials provided with the distribution.
1356160Sru#
1456160Sru# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1556160Sru# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1656160Sru# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1756160Sru# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1856160Sru# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1956160Sru# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2056160Sru# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2156160Sru# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2256160Sru# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2356160Sru# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2456160Sru# SUCH DAMAGE.
2556160Sru#
2656160Sru# $FreeBSD: head/sys/kern/bus_if.m 295755 2016-02-18 13:00:04Z zbb $
27146515Sru#
28146515Sru
29146515Sru#include <sys/types.h>
30146515Sru#include <sys/systm.h>
31146515Sru#include <sys/bus.h>
3256160Sru
33146515Sru/**
34146515Sru * @defgroup BUS bus - KObj methods for drivers of devices with children
35146515Sru * @brief A set of methods required device drivers that support
36146515Sru * child devices.
3756160Sru * @{
38146515Sru */
39146515SruINTERFACE bus;
40146515Sru
41146515Sru#
4256160Sru# Default implementations of some methods.
43146515Sru#
44146515SruCODE {
45146515Sru	static struct resource *
46146515Sru	null_alloc_resource(device_t dev, device_t child,
4756160Sru	    int type, int *rid, rman_res_t start, rman_res_t end,
48146515Sru	    rman_res_t count, u_int flags)
49146515Sru	{
50146515Sru	    return (0);
51146515Sru	}
52146515Sru
53146515Sru	static int
5456160Sru	null_remap_intr(device_t bus, device_t dev, u_int irq)
55146515Sru	{
56146515Sru
57146515Sru		if (dev != NULL)
5856160Sru			return (BUS_REMAP_INTR(dev, NULL, irq));
59146515Sru		return (ENXIO);
60146515Sru	}
61146515Sru
6256160Sru	static device_t
63146515Sru	null_add_child(device_t bus, int order, const char *name,
64146515Sru	    int unit)
65146515Sru	{
66146515Sru
67146515Sru		panic("bus_add_child is not implemented");
68146515Sru	}
69146515Sru};
70146515Sru
71146515Sru/**
7256160Sru * @brief Print a description of a child device
7356160Sru *
7456160Sru * This is called from system code which prints out a description of a
7556160Sru * device. It should describe the attachment that the child has with
7656160Sru * the parent. For instance the TurboLaser bus prints which node the
7756160Sru * device is attached to. See bus_generic_print_child() for more 
7856160Sru * information.
7956160Sru *
8056160Sru * @param _dev		the device whose child is being printed
8156160Sru * @param _child	the child device to describe
8256160Sru *
8356160Sru * @returns		the number of characters output.
8456160Sru */
8556160SruMETHOD int print_child {
8656160Sru	device_t _dev;
8756160Sru	device_t _child;
8856160Sru} DEFAULT bus_generic_print_child;
8956160Sru
9056160Sru/**
9156160Sru * @brief Print a notification about an unprobed child device.
9256160Sru *
9356160Sru * Called for each child device that did not succeed in probing for a
9456160Sru * driver.
9556160Sru *
9656160Sru * @param _dev		the device whose child was being probed
9756160Sru * @param _child	the child device which failed to probe
9856160Sru */   
9956160SruMETHOD void probe_nomatch {
10056160Sru        device_t _dev;
10156160Sru        device_t _child;
10256160Sru};
10356160Sru
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. Implementations of bus_add_child
213 * call device_add_child_ordered to add the child and often add
214 * a suitable ivar to the device specific to that bus.
215 * 
216 * @param _dev		the bus device which will be the parent of the
217 *			new child device
218 * @param _order	a value which is used to partially sort the
219 *			children of @p _dev - devices created using
220 *			lower values of @p _order appear first in @p
221 *			_dev's list of children
222 * @param _name		devclass name for new device or @c NULL if not
223 *			specified
224 * @param _unit		unit number for new device or @c -1 if not
225 *			specified
226 */
227METHOD device_t add_child {
228	device_t _dev;
229	u_int _order;
230	const char *_name;
231	int _unit;
232} DEFAULT null_add_child;
233
234/**
235 * @brief Allocate a system resource
236 *
237 * This method is called by child devices of a bus to allocate resources.
238 * The types are defined in <machine/resource.h>; the meaning of the
239 * resource-ID field varies from bus to bus (but @p *rid == 0 is always
240 * valid if the resource type is). If a resource was allocated and the
241 * caller did not use the RF_ACTIVE to specify that it should be
242 * activated immediately, the caller is responsible for calling
243 * BUS_ACTIVATE_RESOURCE() when it actually uses the resource.
244 *
245 * @param _dev		the parent device of @p _child
246 * @param _child	the device which is requesting an allocation
247 * @param _type		the type of resource to allocate
248 * @param _rid		a pointer to the resource identifier
249 * @param _start	hint at the start of the resource range - pass
250 *			@c 0UL for any start address
251 * @param _end		hint at the end of the resource range - pass
252 *			@c ~0UL for any end address
253 * @param _count	hint at the size of range required - pass @c 1
254 *			for any size
255 * @param _flags	any extra flags to control the resource
256 *			allocation - see @c RF_XXX flags in
257 *			<sys/rman.h> for details
258 * 
259 * @returns		the resource which was allocated or @c NULL if no
260 *			resource could be allocated
261 */
262METHOD struct resource * alloc_resource {
263	device_t	_dev;
264	device_t	_child;
265	int		_type;
266	int	       *_rid;
267	rman_res_t	_start;
268	rman_res_t	_end;
269	rman_res_t	_count;
270	u_int		_flags;
271} DEFAULT null_alloc_resource;
272
273/**
274 * @brief Activate a resource
275 *
276 * Activate a resource previously allocated with
277 * BUS_ALLOC_RESOURCE(). This may for instance map a memory region
278 * into the kernel's virtual address space.
279 *
280 * @param _dev		the parent device of @p _child
281 * @param _child	the device which allocated the resource
282 * @param _type		the type of resource
283 * @param _rid		the resource identifier
284 * @param _r		the resource to activate
285 */
286METHOD int activate_resource {
287	device_t	_dev;
288	device_t	_child;
289	int		_type;
290	int		_rid;
291	struct resource *_r;
292};
293
294/**
295 * @brief Deactivate a resource
296 *
297 * Deactivate a resource previously allocated with
298 * BUS_ALLOC_RESOURCE(). This may for instance unmap a memory region
299 * from the kernel's virtual address space.
300 *
301 * @param _dev		the parent device of @p _child
302 * @param _child	the device which allocated the resource
303 * @param _type		the type of resource
304 * @param _rid		the resource identifier
305 * @param _r		the resource to deactivate
306 */
307METHOD int deactivate_resource {
308	device_t	_dev;
309	device_t	_child;
310	int		_type;
311	int		_rid;
312	struct resource *_r;
313};
314
315/**
316 * @brief Adjust a resource
317 *
318 * Adjust the start and/or end of a resource allocated by
319 * BUS_ALLOC_RESOURCE.  At least part of the new address range must overlap
320 * with the existing address range.  If the successful, the resource's range
321 * will be adjusted to [start, end] on return.
322 *
323 * @param _dev		the parent device of @p _child
324 * @param _child	the device which allocated the resource
325 * @param _type		the type of resource
326 * @param _res		the resource to adjust
327 * @param _start	the new starting address of the resource range
328 * @param _end		the new ending address of the resource range
329 */
330METHOD int adjust_resource {
331	device_t	_dev;
332	device_t	_child;
333	int		_type;
334	struct resource *_res;
335	rman_res_t	_start;
336	rman_res_t	_end;
337};
338
339/**
340 * @brief Release a resource
341 *
342 * Free a resource allocated by the BUS_ALLOC_RESOURCE.  The @p _rid
343 * value must be the same as the one returned by BUS_ALLOC_RESOURCE()
344 * (which is not necessarily the same as the one the client passed).
345 *
346 * @param _dev		the parent device of @p _child
347 * @param _child	the device which allocated the resource
348 * @param _type		the type of resource
349 * @param _rid		the resource identifier
350 * @param _r		the resource to release
351 */
352METHOD int release_resource {
353	device_t	_dev;
354	device_t	_child;
355	int		_type;
356	int		_rid;
357	struct resource *_res;
358};
359
360/**
361 * @brief Install an interrupt handler
362 *
363 * This method is used to associate an interrupt handler function with
364 * an irq resource. When the interrupt triggers, the function @p _intr
365 * will be called with the value of @p _arg as its single
366 * argument. The value returned in @p *_cookiep is used to cancel the
367 * interrupt handler - the caller should save this value to use in a
368 * future call to BUS_TEARDOWN_INTR().
369 * 
370 * @param _dev		the parent device of @p _child
371 * @param _child	the device which allocated the resource
372 * @param _irq		the resource representing the interrupt
373 * @param _flags	a set of bits from enum intr_type specifying
374 *			the class of interrupt
375 * @param _intr		the function to call when the interrupt
376 *			triggers
377 * @param _arg		a value to use as the single argument in calls
378 *			to @p _intr
379 * @param _cookiep	a pointer to a location to recieve a cookie
380 *			value that may be used to remove the interrupt
381 *			handler
382 */
383METHOD int setup_intr {
384	device_t	_dev;
385	device_t	_child;
386	struct resource *_irq;
387	int		_flags;
388	driver_filter_t	*_filter;
389	driver_intr_t	*_intr;
390	void		*_arg;
391	void		**_cookiep;
392};
393
394/**
395 * @brief Uninstall an interrupt handler
396 *
397 * This method is used to disassociate an interrupt handler function
398 * with an irq resource. The value of @p _cookie must be the value
399 * returned from a previous call to BUS_SETUP_INTR().
400 * 
401 * @param _dev		the parent device of @p _child
402 * @param _child	the device which allocated the resource
403 * @param _irq		the resource representing the interrupt
404 * @param _cookie	the cookie value returned when the interrupt
405 *			was originally registered
406 */
407METHOD int teardown_intr {
408	device_t	_dev;
409	device_t	_child;
410	struct resource	*_irq;
411	void		*_cookie;
412};
413
414/**
415 * @brief Define a resource which can be allocated with
416 * BUS_ALLOC_RESOURCE().
417 *
418 * This method is used by some busses (typically ISA) to allow a
419 * driver to describe a resource range that it would like to
420 * allocate. The resource defined by @p _type and @p _rid is defined
421 * to start at @p _start and to include @p _count indices in its
422 * range.
423 * 
424 * @param _dev		the parent device of @p _child
425 * @param _child	the device which owns the resource
426 * @param _type		the type of resource
427 * @param _rid		the resource identifier
428 * @param _start	the start of the resource range
429 * @param _count	the size of the resource range
430 */
431METHOD int set_resource {
432	device_t	_dev;
433	device_t	_child;
434	int		_type;
435	int		_rid;
436	rman_res_t	_start;
437	rman_res_t	_count;
438};
439
440/**
441 * @brief Describe a resource
442 *
443 * This method allows a driver to examine the range used for a given
444 * resource without actually allocating it.
445 * 
446 * @param _dev		the parent device of @p _child
447 * @param _child	the device which owns the resource
448 * @param _type		the type of resource
449 * @param _rid		the resource identifier
450 * @param _start	the address of a location to recieve the start
451 *			index of the resource range
452 * @param _count	the address of a location to recieve the size
453 *			of the resource range
454 */
455METHOD int get_resource {
456	device_t	_dev;
457	device_t	_child;
458	int		_type;
459	int		_rid;
460	rman_res_t	*_startp;
461	rman_res_t	*_countp;
462};
463
464/**
465 * @brief Delete a resource.
466 * 
467 * Use this to delete a resource (possibly one previously added with
468 * BUS_SET_RESOURCE()).
469 * 
470 * @param _dev		the parent device of @p _child
471 * @param _child	the device which owns the resource
472 * @param _type		the type of resource
473 * @param _rid		the resource identifier
474 */
475METHOD void delete_resource {
476	device_t	_dev;
477	device_t	_child;
478	int		_type;
479	int		_rid;
480};
481
482/**
483 * @brief Return a struct resource_list.
484 *
485 * Used by drivers which use bus_generic_rl_alloc_resource() etc. to
486 * implement their resource handling. It should return the resource
487 * list of the given child device.
488 * 
489 * @param _dev		the parent device of @p _child
490 * @param _child	the device which owns the resource list
491 */
492METHOD struct resource_list * get_resource_list {
493	device_t	_dev;
494	device_t	_child;
495} DEFAULT bus_generic_get_resource_list;
496
497/**
498 * @brief Is the hardware described by @p _child still attached to the
499 * system?
500 *
501 * This method should return 0 if the device is not present.  It
502 * should return -1 if it is present.  Any errors in determining
503 * should be returned as a normal errno value.  Client drivers are to
504 * assume that the device is present, even if there is an error
505 * determining if it is there.  Busses are to try to avoid returning
506 * errors, but newcard will return an error if the device fails to
507 * implement this method.
508 * 
509 * @param _dev		the parent device of @p _child
510 * @param _child	the device which is being examined
511 */
512METHOD int child_present {
513	device_t	_dev;
514	device_t	_child;
515} DEFAULT bus_generic_child_present;
516
517/**
518 * @brief Returns the pnp info for this device.
519 *
520 * Return it as a string.  If the string is insufficient for the
521 * storage, then return EOVERFLOW.
522 * 
523 * @param _dev		the parent device of @p _child
524 * @param _child	the device which is being examined
525 * @param _buf		the address of a buffer to receive the pnp
526 *			string
527 * @param _buflen	the size of the buffer pointed to by @p _buf
528 */
529METHOD int child_pnpinfo_str {
530	device_t	_dev;
531	device_t	_child;
532	char		*_buf;
533	size_t		_buflen;
534};
535
536/**
537 * @brief Returns the location for this device.
538 *
539 * Return it as a string.  If the string is insufficient for the
540 * storage, then return EOVERFLOW.
541 * 
542 * @param _dev		the parent device of @p _child
543 * @param _child	the device which is being examined
544 * @param _buf		the address of a buffer to receive the location
545 *			string
546 * @param _buflen	the size of the buffer pointed to by @p _buf
547 */
548METHOD int child_location_str {
549	device_t	_dev;
550	device_t	_child;
551	char		*_buf;
552	size_t		_buflen;
553};
554
555/**
556 * @brief Allow drivers to request that an interrupt be bound to a specific
557 * CPU.
558 * 
559 * @param _dev		the parent device of @p _child
560 * @param _child	the device which allocated the resource
561 * @param _irq		the resource representing the interrupt
562 * @param _cpu		the CPU to bind the interrupt to
563 */
564METHOD int bind_intr {
565	device_t	_dev;
566	device_t	_child;
567	struct resource *_irq;
568	int		_cpu;
569} DEFAULT bus_generic_bind_intr;
570
571/**
572 * @brief Allow (bus) drivers to specify the trigger mode and polarity
573 * of the specified interrupt.
574 * 
575 * @param _dev		the bus device
576 * @param _irq		the interrupt number to modify
577 * @param _trig		the trigger mode required
578 * @param _pol		the interrupt polarity required
579 */
580METHOD int config_intr {
581	device_t	_dev;
582	int		_irq;
583	enum intr_trigger _trig;
584	enum intr_polarity _pol;
585} DEFAULT bus_generic_config_intr;
586
587/**
588 * @brief Allow drivers to associate a description with an active
589 * interrupt handler.
590 *
591 * @param _dev		the parent device of @p _child
592 * @param _child	the device which allocated the resource
593 * @param _irq		the resource representing the interrupt
594 * @param _cookie	the cookie value returned when the interrupt
595 *			was originally registered
596 * @param _descr	the description to associate with the interrupt
597 */
598METHOD int describe_intr {
599	device_t	_dev;
600	device_t	_child;
601	struct resource *_irq;
602	void		*_cookie;
603	const char	*_descr;
604} DEFAULT bus_generic_describe_intr;
605
606/**
607 * @brief Notify a (bus) driver about a child that the hints mechanism
608 * believes it has discovered.
609 *
610 * The bus is responsible for then adding the child in the right order
611 * and discovering other things about the child.  The bus driver is
612 * free to ignore this hint, to do special things, etc.  It is all up
613 * to the bus driver to interpret.
614 *
615 * This method is only called in response to the parent bus asking for
616 * hinted devices to be enumerated.
617 *
618 * @param _dev		the bus device
619 * @param _dname	the name of the device w/o unit numbers
620 * @param _dunit	the unit number of the device
621 */
622METHOD void hinted_child {
623	device_t	_dev;
624	const char	*_dname;
625	int		_dunit;
626};
627
628/**
629 * @brief Returns bus_dma_tag_t for use w/ devices on the bus.
630 *
631 * @param _dev		the parent device of @p _child
632 * @param _child	the device to which the tag will belong
633 */
634METHOD bus_dma_tag_t get_dma_tag {
635	device_t	_dev;
636	device_t	_child;
637} DEFAULT bus_generic_get_dma_tag;
638
639/**
640 * @brief Returns bus_space_tag_t for use w/ devices on the bus.
641 *
642 * @param _dev		the parent device of @p _child
643 * @param _child	the device to which the tag will belong
644 */
645METHOD bus_space_tag_t get_bus_tag {
646	device_t	_dev;
647	device_t	_child;
648} DEFAULT bus_generic_get_bus_tag;
649
650/**
651 * @brief Allow the bus to determine the unit number of a device.
652 *
653 * @param _dev		the parent device of @p _child
654 * @param _child	the device whose unit is to be wired
655 * @param _name		the name of the device's new devclass
656 * @param _unitp	a pointer to the device's new unit value
657 */
658METHOD void hint_device_unit {
659	device_t	_dev;
660	device_t	_child;
661	const char	*_name;
662	int		*_unitp;
663};
664
665/**
666 * @brief Notify a bus that the bus pass level has been changed
667 *
668 * @param _dev		the bus device
669 */
670METHOD void new_pass {
671	device_t	_dev;
672} DEFAULT bus_generic_new_pass;
673
674/**
675 * @brief Notify a bus that specified child's IRQ should be remapped.
676 *
677 * @param _dev		the bus device
678 * @param _child	the child device
679 * @param _irq		the irq number
680 */
681METHOD int remap_intr {
682	device_t	_dev;
683	device_t	_child;
684	u_int		_irq;
685} DEFAULT null_remap_intr;
686
687/**
688 * @brief Suspend a given child
689 *
690 * @param _dev		the parent device of @p _child
691 * @param _child	the device to suspend
692 */
693METHOD int suspend_child {
694	device_t	_dev;
695	device_t	_child;
696} DEFAULT bus_generic_suspend_child;
697
698/**
699 * @brief Resume a given child
700 *
701 * @param _dev		the parent device of @p _child
702 * @param _child	the device to resume
703 */
704METHOD int resume_child {
705	device_t	_dev;
706	device_t	_child;
707} DEFAULT bus_generic_resume_child;
708
709/**
710 * @brief Get the VM domain handle for the given bus and child.
711 *
712 * @param _dev		the bus device
713 * @param _child	the child device
714 * @param _domain	a pointer to the bus's domain handle identifier
715 */
716METHOD int get_domain {
717	device_t	_dev;
718	device_t	_child;
719	int		*_domain;
720} DEFAULT bus_generic_get_domain;
721