2 * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
4 * This program is free software and is provided to you under the terms of the GNU General Public License version 2
5 * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
7 * A copy of the licence is included with the program, and can also be obtained from Free Software
8 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
13 * Defines the OS abstraction layer for the kernel device driver (OSK)
16 #ifndef __MALI_OSK_H__
17 #define __MALI_OSK_H__
19 #include "mali_osk_types.h"
20 #include "mali_osk_specific.h" /* include any per-os specifics */
21 #include "mali_osk_locks.h"
28 * @addtogroup uddapi Unified Device Driver (UDD) APIs
34 * @addtogroup oskapi UDD OS Abstraction for Kernel-side (OSK) APIs
39 /** @addtogroup _mali_osk_lock OSK Mutual Exclusion Locks
43 /** @brief Macro for asserting that the current thread holds a given lock
45 #define MALI_DEBUG_ASSERT_LOCK_HELD(l) MALI_DEBUG_ASSERT(_mali_osk_lock_get_owner((_mali_osk_lock_debug_t *)l) == _mali_osk_get_tid());
47 /** @brief returns a lock's owner (thread id) if debugging is enabled
50 #define MALI_DEBUG_ASSERT_LOCK_HELD(l) do {} while(0)
53 /** @} */ /* end group _mali_osk_lock */
55 /** @addtogroup _mali_osk_miscellaneous
58 /** @brief Find the containing structure of another structure
60 * This is the reverse of the operation 'offsetof'. This means that the
61 * following condition is satisfied:
63 * ptr == _MALI_OSK_CONTAINER_OF( &ptr->member, type, member )
65 * When ptr is of type 'type'.
67 * Its purpose it to recover a larger structure that has wrapped a smaller one.
69 * @note no type or memory checking occurs to ensure that a wrapper structure
70 * does in fact exist, and that it is being recovered with respect to the
73 * @param ptr the pointer to the member that is contained within the larger
75 * @param type the type of the structure that contains the member
76 * @param member the name of the member in the structure that ptr points to.
77 * @return a pointer to a \a type object which contains \a member, as pointed
80 #define _MALI_OSK_CONTAINER_OF(ptr, type, member) \
81 ((type *)( ((char *)ptr) - offsetof(type,member) ))
83 /** @addtogroup _mali_osk_wq
86 /** @brief Initialize work queues (for deferred work)
88 * @return _MALI_OSK_ERR_OK on success, otherwise failure.
90 _mali_osk_errcode_t _mali_osk_wq_init(void);
92 /** @brief Terminate work queues (for deferred work)
94 void _mali_osk_wq_term(void);
96 /** @brief Create work in the work queue
98 * Creates a work object which can be scheduled in the work queue. When
99 * scheduled, \a handler will be called with \a data as the argument.
101 * Refer to \ref _mali_osk_wq_schedule_work() for details on how work
102 * is scheduled in the queue.
104 * The returned pointer must be freed with \ref _mali_osk_wq_delete_work()
105 * when no longer needed.
107 _mali_osk_wq_work_t *_mali_osk_wq_create_work(_mali_osk_wq_work_handler_t handler, void *data);
109 /** @brief A high priority version of \a _mali_osk_wq_create_work()
111 * Creates a work object which can be scheduled in the high priority work queue.
113 * This is unfortunately needed to get low latency scheduling of the Mali cores. Normally we would
114 * schedule the next job in hw_irq or tasklet, but often we can't since we need to synchronously map
115 * and unmap shared memory when a job is connected to external fences (timelines). And this requires
118 * We do signal a lot of other (low priority) work also as part of the job being finished, and if we
119 * don't set this Mali scheduling thread as high priority, we see that the CPU scheduler often runs
120 * random things instead of starting the next GPU job when the GPU is idle. So setting the gpu
121 * scheduler to high priority does give a visually more responsive system.
123 * Start the high priority work with: \a _mali_osk_wq_schedule_work_high_pri()
125 _mali_osk_wq_work_t *_mali_osk_wq_create_work_high_pri(_mali_osk_wq_work_handler_t handler, void *data);
127 /** @brief Delete a work object
129 * This will flush the work queue to ensure that the work handler will not
130 * be called after deletion.
132 void _mali_osk_wq_delete_work(_mali_osk_wq_work_t *work);
134 /** @brief Delete a work object
136 * This will NOT flush the work queue, so only call this if you are sure that the work handler will
137 * not be called after deletion.
139 void _mali_osk_wq_delete_work_nonflush(_mali_osk_wq_work_t *work);
141 /** @brief Cause a queued, deferred call of the work handler
143 * _mali_osk_wq_schedule_work provides a mechanism for enqueuing deferred calls
144 * to the work handler. After calling \ref _mali_osk_wq_schedule_work(), the
145 * work handler will be scheduled to run at some point in the future.
147 * Typically this is called by the IRQ upper-half to defer further processing of
148 * IRQ-related work to the IRQ bottom-half handler. This is necessary for work
149 * that cannot be done in an IRQ context by the IRQ upper-half handler. Timer
150 * callbacks also use this mechanism, because they are treated as though they
151 * operate in an IRQ context. Refer to \ref _mali_osk_timer_t for more
154 * Code that operates in a kernel-process context (with no IRQ context
155 * restrictions) may also enqueue deferred calls to the IRQ bottom-half. The
156 * advantage over direct calling is that deferred calling allows the caller and
157 * IRQ bottom half to hold the same mutex, with a guarantee that they will not
158 * deadlock just by using this mechanism.
160 * _mali_osk_wq_schedule_work() places deferred call requests on a queue, to
161 * allow for more than one thread to make a deferred call. Therfore, if it is
162 * called 'K' times, then the IRQ bottom-half will be scheduled 'K' times too.
163 * 'K' is a number that is implementation-specific.
165 * _mali_osk_wq_schedule_work() is guaranteed to not block on:
166 * - enqueuing a deferred call request.
167 * - the completion of the work handler.
169 * This is to prevent deadlock. For example, if _mali_osk_wq_schedule_work()
170 * blocked, then it would cause a deadlock when the following two conditions
172 * - The work handler callback (of type _mali_osk_wq_work_handler_t) locks
174 * - And, at the same time, the caller of _mali_osk_wq_schedule_work() also
175 * holds the same mutex
177 * @note care must be taken to not overflow the queue that
178 * _mali_osk_wq_schedule_work() operates on. Code must be structured to
179 * ensure that the number of requests made to the queue is bounded. Otherwise,
182 * The queue that _mali_osk_wq_schedule_work implements is a FIFO of N-writer,
183 * 1-reader type. The writers are the callers of _mali_osk_wq_schedule_work
184 * (all OSK-registered IRQ upper-half handlers in the system, watchdog timers,
185 * callers from a Kernel-process context). The reader is a single thread that
186 * handles all OSK-registered work.
188 * @param work a pointer to the _mali_osk_wq_work_t object corresponding to the
189 * work to begin processing.
191 void _mali_osk_wq_schedule_work(_mali_osk_wq_work_t *work);
193 /** @brief Cause a queued, deferred call of the high priority work handler
195 * Function is the same as \a _mali_osk_wq_schedule_work() with the only
196 * difference that it runs in a high (real time) priority on the system.
198 * Should only be used as a substitue for doing the same work in interrupts.
200 * This is allowed to sleep, but the work should be small since it will block
201 * all other applications.
203 void _mali_osk_wq_schedule_work_high_pri(_mali_osk_wq_work_t *work);
205 /** @brief Flush the work queue
207 * This will flush the OSK work queue, ensuring all work in the queue has
208 * completed before returning.
210 * Since this blocks on the completion of work in the work-queue, the
211 * caller of this function \b must \b not hold any mutexes that are taken by
212 * any registered work handler. To do so may cause a deadlock.
215 void _mali_osk_wq_flush(void);
217 /** @brief Create work in the delayed work queue
219 * Creates a work object which can be scheduled in the work queue. When
220 * scheduled, a timer will be start and the \a handler will be called with
221 * \a data as the argument when timer out
223 * Refer to \ref _mali_osk_wq_delayed_schedule_work() for details on how work
224 * is scheduled in the queue.
226 * The returned pointer must be freed with \ref _mali_osk_wq_delayed_delete_work_nonflush()
227 * when no longer needed.
229 _mali_osk_wq_delayed_work_t *_mali_osk_wq_delayed_create_work(_mali_osk_wq_work_handler_t handler, void *data);
231 /** @brief Delete a work object
233 * This will NOT flush the work queue, so only call this if you are sure that the work handler will
234 * not be called after deletion.
236 void _mali_osk_wq_delayed_delete_work_nonflush(_mali_osk_wq_delayed_work_t *work);
238 /** @brief Cancel a delayed work without waiting for it to finish
240 * Note that the \a work callback function may still be running on return from
241 * _mali_osk_wq_delayed_cancel_work_async().
243 * @param work The delayed work to be cancelled
245 void _mali_osk_wq_delayed_cancel_work_async(_mali_osk_wq_delayed_work_t *work);
247 /** @brief Cancel a delayed work and wait for it to finish
249 * When this function returns, the \a work was either cancelled or it finished running.
251 * @param work The delayed work to be cancelled
253 void _mali_osk_wq_delayed_cancel_work_sync(_mali_osk_wq_delayed_work_t *work);
255 /** @brief Put \a work task in global workqueue after delay
257 * After waiting for a given time this puts a job in the kernel-global
260 * If \a work was already on a queue, this function will return without doing anything
262 * @param work job to be done
263 * @param delay number of jiffies to wait or 0 for immediate execution
265 void _mali_osk_wq_delayed_schedule_work(_mali_osk_wq_delayed_work_t *work, u32 delay);
267 /** @} */ /* end group _mali_osk_wq */
270 /** @addtogroup _mali_osk_irq
273 /** @brief Initialize IRQ handling for a resource
275 * Registers an interrupt handler \a uhandler for the given IRQ number \a irqnum.
276 * \a data will be passed as argument to the handler when an interrupt occurs.
278 * If \a irqnum is -1, _mali_osk_irq_init will probe for the IRQ number using
279 * the supplied \a trigger_func and \a ack_func. These functions will also
280 * receive \a data as their argument.
282 * @param irqnum The IRQ number that the resource uses, as seen by the CPU.
283 * The value -1 has a special meaning which indicates the use of probing, and
284 * trigger_func and ack_func must be non-NULL.
285 * @param uhandler The interrupt handler, corresponding to a ISR handler for
287 * @param int_data resource specific data, which will be passed to uhandler
288 * @param trigger_func Optional: a function to trigger the resource's irq, to
289 * probe for the interrupt. Use NULL if irqnum != -1.
290 * @param ack_func Optional: a function to acknowledge the resource's irq, to
291 * probe for the interrupt. Use NULL if irqnum != -1.
292 * @param probe_data resource-specific data, which will be passed to
293 * (if present) trigger_func and ack_func
294 * @param description textual description of the IRQ resource.
295 * @return on success, a pointer to a _mali_osk_irq_t object, which represents
296 * the IRQ handling on this resource. NULL on failure.
298 _mali_osk_irq_t *_mali_osk_irq_init(u32 irqnum, _mali_osk_irq_uhandler_t uhandler, void *int_data, _mali_osk_irq_trigger_t trigger_func, _mali_osk_irq_ack_t ack_func, void *probe_data, const char *description);
300 /** @brief Terminate IRQ handling on a resource.
302 * This will disable the interrupt from the device, and then waits for any
303 * currently executing IRQ handlers to complete.
305 * @note If work is deferred to an IRQ bottom-half handler through
306 * \ref _mali_osk_wq_schedule_work(), be sure to flush any remaining work
307 * with \ref _mali_osk_wq_flush() or (implicitly) with \ref _mali_osk_wq_delete_work()
309 * @param irq a pointer to the _mali_osk_irq_t object corresponding to the
310 * resource whose IRQ handling is to be terminated.
312 void _mali_osk_irq_term(_mali_osk_irq_t *irq);
314 /** @} */ /* end group _mali_osk_irq */
317 /** @addtogroup _mali_osk_atomic
320 /** @brief Decrement an atomic counter
322 * @note It is an error to decrement the counter beyond -(1<<23)
324 * @param atom pointer to an atomic counter */
325 void _mali_osk_atomic_dec(_mali_osk_atomic_t *atom);
327 /** @brief Decrement an atomic counter, return new value
329 * @param atom pointer to an atomic counter
330 * @return The new value, after decrement */
331 u32 _mali_osk_atomic_dec_return(_mali_osk_atomic_t *atom);
333 /** @brief Increment an atomic counter
335 * @note It is an error to increment the counter beyond (1<<23)-1
337 * @param atom pointer to an atomic counter */
338 void _mali_osk_atomic_inc(_mali_osk_atomic_t *atom);
340 /** @brief Increment an atomic counter, return new value
342 * @param atom pointer to an atomic counter */
343 u32 _mali_osk_atomic_inc_return(_mali_osk_atomic_t *atom);
345 /** @brief Initialize an atomic counter
347 * @note the parameter required is a u32, and so signed integers should be
350 * @param atom pointer to an atomic counter
351 * @param val the value to initialize the atomic counter.
353 void _mali_osk_atomic_init(_mali_osk_atomic_t *atom, u32 val);
355 /** @brief Read a value from an atomic counter
357 * This can only be safely used to determine the value of the counter when it
358 * is guaranteed that other threads will not be modifying the counter. This
359 * makes its usefulness limited.
361 * @param atom pointer to an atomic counter
363 u32 _mali_osk_atomic_read(_mali_osk_atomic_t *atom);
365 /** @brief Terminate an atomic counter
367 * @param atom pointer to an atomic counter
369 void _mali_osk_atomic_term(_mali_osk_atomic_t *atom);
371 /** @brief Assign a new val to atomic counter, and return the old atomic counter
373 * @param atom pointer to an atomic counter
374 * @param val the new value assign to the atomic counter
375 * @return the old value of the atomic counter
377 u32 _mali_osk_atomic_xchg(_mali_osk_atomic_t *atom, u32 val);
378 /** @} */ /* end group _mali_osk_atomic */
381 /** @defgroup _mali_osk_memory OSK Memory Allocation
384 /** @brief Allocate zero-initialized memory.
386 * Returns a buffer capable of containing at least \a n elements of \a size
387 * bytes each. The buffer is initialized to zero.
389 * If there is a need for a bigger block of memory (16KB or bigger), then
390 * consider to use _mali_osk_vmalloc() instead, as this function might
391 * map down to a OS function with size limitations.
393 * The buffer is suitably aligned for storage and subsequent access of every
394 * type that the compiler supports. Therefore, the pointer to the start of the
395 * buffer may be cast into any pointer type, and be subsequently accessed from
396 * such a pointer, without loss of information.
398 * When the buffer is no longer in use, it must be freed with _mali_osk_free().
399 * Failure to do so will cause a memory leak.
401 * @note Most toolchains supply memory allocation functions that meet the
402 * compiler's alignment requirements.
404 * @param n Number of elements to allocate
405 * @param size Size of each element
406 * @return On success, the zero-initialized buffer allocated. NULL on failure
408 void *_mali_osk_calloc(u32 n, u32 size);
410 /** @brief Allocate memory.
412 * Returns a buffer capable of containing at least \a size bytes. The
413 * contents of the buffer are undefined.
415 * If there is a need for a bigger block of memory (16KB or bigger), then
416 * consider to use _mali_osk_vmalloc() instead, as this function might
417 * map down to a OS function with size limitations.
419 * The buffer is suitably aligned for storage and subsequent access of every
420 * type that the compiler supports. Therefore, the pointer to the start of the
421 * buffer may be cast into any pointer type, and be subsequently accessed from
422 * such a pointer, without loss of information.
424 * When the buffer is no longer in use, it must be freed with _mali_osk_free().
425 * Failure to do so will cause a memory leak.
427 * @note Most toolchains supply memory allocation functions that meet the
428 * compiler's alignment requirements.
430 * Remember to free memory using _mali_osk_free().
431 * @param size Number of bytes to allocate
432 * @return On success, the buffer allocated. NULL on failure.
434 void *_mali_osk_malloc(u32 size);
436 /** @brief Free memory.
438 * Reclaims the buffer pointed to by the parameter \a ptr for the system.
439 * All memory returned from _mali_osk_malloc() and _mali_osk_calloc()
440 * must be freed before the application exits. Otherwise,
441 * a memory leak will occur.
443 * Memory must be freed once. It is an error to free the same non-NULL pointer
446 * It is legal to free the NULL pointer.
448 * @param ptr Pointer to buffer to free
450 void _mali_osk_free(void *ptr);
452 /** @brief Allocate memory.
454 * Returns a buffer capable of containing at least \a size bytes. The
455 * contents of the buffer are undefined.
457 * This function is potentially slower than _mali_osk_malloc() and _mali_osk_calloc(),
458 * but do support bigger sizes.
460 * The buffer is suitably aligned for storage and subsequent access of every
461 * type that the compiler supports. Therefore, the pointer to the start of the
462 * buffer may be cast into any pointer type, and be subsequently accessed from
463 * such a pointer, without loss of information.
465 * When the buffer is no longer in use, it must be freed with _mali_osk_free().
466 * Failure to do so will cause a memory leak.
468 * @note Most toolchains supply memory allocation functions that meet the
469 * compiler's alignment requirements.
471 * Remember to free memory using _mali_osk_free().
472 * @param size Number of bytes to allocate
473 * @return On success, the buffer allocated. NULL on failure.
475 void *_mali_osk_valloc(u32 size);
477 /** @brief Free memory.
479 * Reclaims the buffer pointed to by the parameter \a ptr for the system.
480 * All memory returned from _mali_osk_valloc() must be freed before the
481 * application exits. Otherwise a memory leak will occur.
483 * Memory must be freed once. It is an error to free the same non-NULL pointer
486 * It is legal to free the NULL pointer.
488 * @param ptr Pointer to buffer to free
490 void _mali_osk_vfree(void *ptr);
492 /** @brief Copies memory.
494 * Copies the \a len bytes from the buffer pointed by the parameter \a src
495 * directly to the buffer pointed by \a dst.
497 * It is an error for \a src to overlap \a dst anywhere in \a len bytes.
499 * @param dst Pointer to the destination array where the content is to be
501 * @param src Pointer to the source of data to be copied.
502 * @param len Number of bytes to copy.
503 * @return \a dst is always passed through unmodified.
505 void *_mali_osk_memcpy(void *dst, const void *src, u32 len);
507 /** @brief Fills memory.
509 * Sets the first \a n bytes of the block of memory pointed to by \a s to
510 * the specified value
511 * @param s Pointer to the block of memory to fill.
512 * @param c Value to be set, passed as u32. Only the 8 Least Significant Bits (LSB)
514 * @param n Number of bytes to be set to the value.
515 * @return \a s is always passed through unmodified
517 void *_mali_osk_memset(void *s, u32 c, u32 n);
518 /** @} */ /* end group _mali_osk_memory */
521 /** @brief Checks the amount of memory allocated
523 * Checks that not more than \a max_allocated bytes are allocated.
525 * Some OS bring up an interactive out of memory dialogue when the
526 * system runs out of memory. This can stall non-interactive
527 * apps (e.g. automated test runs). This function can be used to
528 * not trigger the OOM dialogue by keeping allocations
529 * within a certain limit.
531 * @return MALI_TRUE when \a max_allocated bytes are not in use yet. MALI_FALSE
532 * when at least \a max_allocated bytes are in use.
534 mali_bool _mali_osk_mem_check_allocated(u32 max_allocated);
537 /** @addtogroup _mali_osk_low_level_memory
540 /** @brief Issue a memory barrier
542 * This defines an arbitrary memory barrier operation, which forces an ordering constraint
543 * on memory read and write operations.
545 void _mali_osk_mem_barrier(void);
547 /** @brief Issue a write memory barrier
549 * This defines an write memory barrier operation which forces an ordering constraint
550 * on memory write operations.
552 void _mali_osk_write_mem_barrier(void);
554 /** @brief Map a physically contiguous region into kernel space
556 * This is primarily used for mapping in registers from resources, and Mali-MMU
557 * page tables. The mapping is only visable from kernel-space.
559 * Access has to go through _mali_osk_mem_ioread32 and _mali_osk_mem_iowrite32
561 * @param phys CPU-physical base address of the memory to map in. This must
562 * be aligned to the system's page size, which is assumed to be 4K.
563 * @param size the number of bytes of physically contiguous address space to
565 * @param description A textual description of the memory being mapped in.
566 * @return On success, a Mali IO address through which the mapped-in
567 * memory/registers can be accessed. NULL on failure.
569 mali_io_address _mali_osk_mem_mapioregion(uintptr_t phys, u32 size, const char *description);
571 /** @brief Unmap a physically contiguous address range from kernel space.
573 * The address range should be one previously mapped in through
574 * _mali_osk_mem_mapioregion.
576 * It is a programming error to do (but not limited to) the following:
577 * - attempt an unmap twice
578 * - unmap only part of a range obtained through _mali_osk_mem_mapioregion
579 * - unmap more than the range obtained through _mali_osk_mem_mapioregion
580 * - unmap an address range that was not successfully mapped using
581 * _mali_osk_mem_mapioregion
582 * - provide a mapping that does not map to phys.
584 * @param phys CPU-physical base address of the memory that was originally
585 * mapped in. This must be aligned to the system's page size, which is assumed
587 * @param size The number of bytes that were originally mapped in.
588 * @param mapping The Mali IO address through which the mapping is
591 void _mali_osk_mem_unmapioregion(uintptr_t phys, u32 size, mali_io_address mapping);
593 /** @brief Allocate and Map a physically contiguous region into kernel space
595 * This is used for allocating physically contiguous regions (such as Mali-MMU
596 * page tables) and mapping them into kernel space. The mapping is only
597 * visible from kernel-space.
599 * The alignment of the returned memory is guaranteed to be at least
600 * _MALI_OSK_CPU_PAGE_SIZE.
602 * Access must go through _mali_osk_mem_ioread32 and _mali_osk_mem_iowrite32
604 * @note This function is primarily to provide support for OSs that are
605 * incapable of separating the tasks 'allocate physically contiguous memory'
606 * and 'map it into kernel space'
608 * @param[out] phys CPU-physical base address of memory that was allocated.
609 * (*phys) will be guaranteed to be aligned to at least
610 * _MALI_OSK_CPU_PAGE_SIZE on success.
612 * @param[in] size the number of bytes of physically contiguous memory to
613 * allocate. This must be a multiple of _MALI_OSK_CPU_PAGE_SIZE.
615 * @return On success, a Mali IO address through which the mapped-in
616 * memory/registers can be accessed. NULL on failure, and (*phys) is unmodified.
618 mali_io_address _mali_osk_mem_allocioregion(u32 *phys, u32 size);
620 /** @brief Free a physically contiguous address range from kernel space.
622 * The address range should be one previously mapped in through
623 * _mali_osk_mem_allocioregion.
625 * It is a programming error to do (but not limited to) the following:
626 * - attempt a free twice on the same ioregion
627 * - free only part of a range obtained through _mali_osk_mem_allocioregion
628 * - free more than the range obtained through _mali_osk_mem_allocioregion
629 * - free an address range that was not successfully mapped using
630 * _mali_osk_mem_allocioregion
631 * - provide a mapping that does not map to phys.
633 * @param phys CPU-physical base address of the memory that was originally
634 * mapped in, which was aligned to _MALI_OSK_CPU_PAGE_SIZE.
635 * @param size The number of bytes that were originally mapped in, which was
636 * a multiple of _MALI_OSK_CPU_PAGE_SIZE.
637 * @param mapping The Mali IO address through which the mapping is
640 void _mali_osk_mem_freeioregion(u32 phys, u32 size, mali_io_address mapping);
642 /** @brief Request a region of physically contiguous memory
644 * This is used to ensure exclusive access to a region of physically contigous
647 * It is acceptable to implement this as a stub. However, it is then the job
648 * of the System Integrator to ensure that no other device driver will be using
649 * the physical address ranges used by Mali, while the Mali device driver is
652 * @param phys CPU-physical base address of the memory to request. This must
653 * be aligned to the system's page size, which is assumed to be 4K.
654 * @param size the number of bytes of physically contiguous address space to
656 * @param description A textual description of the memory being requested.
657 * @return _MALI_OSK_ERR_OK on success. Otherwise, a suitable
658 * _mali_osk_errcode_t on failure.
660 _mali_osk_errcode_t _mali_osk_mem_reqregion(uintptr_t phys, u32 size, const char *description);
662 /** @brief Un-request a region of physically contiguous memory
664 * This is used to release a regious of physically contiguous memory previously
665 * requested through _mali_osk_mem_reqregion, so that other device drivers may
666 * use it. This will be called at time of Mali device driver termination.
668 * It is a programming error to attempt to:
669 * - unrequest a region twice
670 * - unrequest only part of a range obtained through _mali_osk_mem_reqregion
671 * - unrequest more than the range obtained through _mali_osk_mem_reqregion
672 * - unrequest an address range that was not successfully requested using
673 * _mali_osk_mem_reqregion
675 * @param phys CPU-physical base address of the memory to un-request. This must
676 * be aligned to the system's page size, which is assumed to be 4K
677 * @param size the number of bytes of physically contiguous address space to
680 void _mali_osk_mem_unreqregion(uintptr_t phys, u32 size);
682 /** @brief Read from a location currently mapped in through
683 * _mali_osk_mem_mapioregion
685 * This reads a 32-bit word from a 32-bit aligned location. It is a programming
686 * error to provide unaligned locations, or to read from memory that is not
687 * mapped in, or not mapped through either _mali_osk_mem_mapioregion() or
688 * _mali_osk_mem_allocioregion().
690 * @param mapping Mali IO address to read from
691 * @param offset Byte offset from the given IO address to operate on, must be a multiple of 4
692 * @return the 32-bit word from the specified location.
694 u32 _mali_osk_mem_ioread32(volatile mali_io_address mapping, u32 offset);
696 /** @brief Write to a location currently mapped in through
697 * _mali_osk_mem_mapioregion without memory barriers
699 * This write a 32-bit word to a 32-bit aligned location without using memory barrier.
700 * It is a programming error to provide unaligned locations, or to write to memory that is not
701 * mapped in, or not mapped through either _mali_osk_mem_mapioregion() or
702 * _mali_osk_mem_allocioregion().
704 * @param mapping Mali IO address to write to
705 * @param offset Byte offset from the given IO address to operate on, must be a multiple of 4
706 * @param val the 32-bit word to write.
708 void _mali_osk_mem_iowrite32_relaxed(volatile mali_io_address addr, u32 offset, u32 val);
710 /** @brief Write to a location currently mapped in through
711 * _mali_osk_mem_mapioregion with write memory barrier
713 * This write a 32-bit word to a 32-bit aligned location. It is a programming
714 * error to provide unaligned locations, or to write to memory that is not
715 * mapped in, or not mapped through either _mali_osk_mem_mapioregion() or
716 * _mali_osk_mem_allocioregion().
718 * @param mapping Mali IO address to write to
719 * @param offset Byte offset from the given IO address to operate on, must be a multiple of 4
720 * @param val the 32-bit word to write.
722 void _mali_osk_mem_iowrite32(volatile mali_io_address mapping, u32 offset, u32 val);
724 /** @brief Flush all CPU caches
726 * This should only be implemented if flushing of the cache is required for
727 * memory mapped in through _mali_osk_mem_mapregion.
729 void _mali_osk_cache_flushall(void);
731 /** @brief Flush any caches necessary for the CPU and MALI to have the same view of a range of uncached mapped memory
733 * This should only be implemented if your OS doesn't do a full cache flush (inner & outer)
734 * after allocating uncached mapped memory.
736 * Some OS do not perform a full cache flush (including all outer caches) for uncached mapped memory.
737 * They zero the memory through a cached mapping, then flush the inner caches but not the outer caches.
738 * This is required for MALI to have the correct view of the memory.
740 void _mali_osk_cache_ensure_uncached_range_flushed(void *uncached_mapping, u32 offset, u32 size);
742 /** @brief Safely copy as much data as possible from src to dest
744 * Do not crash if src or dest isn't available.
746 * @param dest Destination buffer (limited to user space mapped Mali memory)
747 * @param src Source buffer
748 * @param size Number of bytes to copy
749 * @return Number of bytes actually copied
751 u32 _mali_osk_mem_write_safe(void *dest, const void *src, u32 size);
753 /** @} */ /* end group _mali_osk_low_level_memory */
756 /** @addtogroup _mali_osk_notification
758 * User space notification framework
760 * Communication with user space of asynchronous events is performed through a
761 * synchronous call to the \ref u_k_api.
763 * Since the events are asynchronous, the events have to be queued until a
764 * synchronous U/K API call can be made by user-space. A U/K API call might also
765 * be received before any event has happened. Therefore the notifications the
766 * different subsystems wants to send to user space has to be queued for later
767 * reception, or a U/K API call has to be blocked until an event has occured.
769 * Typical uses of notifications are after running of jobs on the hardware or
770 * when changes to the system is detected that needs to be relayed to user
773 * After an event has occured user space has to be notified using some kind of
774 * message. The notification framework supports sending messages to waiting
775 * threads or queueing of messages until a U/K API call is made.
777 * The notification queue is a FIFO. There are no restrictions on the numbers
778 * of readers or writers in the queue.
780 * A message contains what user space needs to identifiy how to handle an
781 * event. This includes a type field and a possible type specific payload.
783 * A notification to user space is represented by a
784 * \ref _mali_osk_notification_t object. A sender gets hold of such an object
785 * using _mali_osk_notification_create(). The buffer given by the
786 * _mali_osk_notification_t::result_buffer field in the object is used to store
787 * any type specific data. The other fields are internal to the queue system
788 * and should not be touched.
792 /** @brief Create a notification object
794 * Returns a notification object which can be added to the queue of
795 * notifications pending for user space transfer.
797 * The implementation will initialize all members of the
798 * \ref _mali_osk_notification_t object. In particular, the
799 * _mali_osk_notification_t::result_buffer member will be initialized to point
800 * to \a size bytes of storage, and that storage will be suitably aligned for
801 * storage of any structure. That is, the created buffer meets the same
802 * requirements as _mali_osk_malloc().
804 * The notification object must be deleted when not in use. Use
805 * _mali_osk_notification_delete() for deleting it.
807 * @note You \b must \b not call _mali_osk_free() on a \ref _mali_osk_notification_t,
808 * object, or on a _mali_osk_notification_t::result_buffer. You must only use
809 * _mali_osk_notification_delete() to free the resources assocaited with a
810 * \ref _mali_osk_notification_t object.
812 * @param type The notification type
813 * @param size The size of the type specific buffer to send
814 * @return Pointer to a notification object with a suitable buffer, or NULL on error.
816 _mali_osk_notification_t *_mali_osk_notification_create(u32 type, u32 size);
818 /** @brief Delete a notification object
820 * This must be called to reclaim the resources of a notification object. This
822 * - The _mali_osk_notification_t::result_buffer
823 * - The \ref _mali_osk_notification_t itself.
825 * A notification object \b must \b not be used after it has been deleted by
826 * _mali_osk_notification_delete().
828 * In addition, the notification object may not be deleted while it is in a
829 * queue. That is, if it has been placed on a queue with
830 * _mali_osk_notification_queue_send(), then it must not be deleted until
831 * it has been received by a call to _mali_osk_notification_queue_receive().
832 * Otherwise, the queue may be corrupted.
834 * @param object the notification object to delete.
836 void _mali_osk_notification_delete(_mali_osk_notification_t *object);
838 /** @brief Create a notification queue
840 * Creates a notification queue which can be used to queue messages for user
841 * delivery and get queued messages from
843 * The queue is a FIFO, and has no restrictions on the numbers of readers or
846 * When the queue is no longer in use, it must be terminated with
847 * \ref _mali_osk_notification_queue_term(). Failure to do so will result in a
850 * @return Pointer to a new notification queue or NULL on error.
852 _mali_osk_notification_queue_t *_mali_osk_notification_queue_init(void);
854 /** @brief Destroy a notification queue
856 * Destroys a notification queue and frees associated resources from the queue.
858 * A notification queue \b must \b not be destroyed in the following cases:
859 * - while there are \ref _mali_osk_notification_t objects in the queue.
860 * - while there are writers currently acting upon the queue. That is, while
861 * a thread is currently calling \ref _mali_osk_notification_queue_send() on
862 * the queue, or while a thread may call
863 * \ref _mali_osk_notification_queue_send() on the queue in the future.
864 * - while there are readers currently waiting upon the queue. That is, while
865 * a thread is currently calling \ref _mali_osk_notification_queue_receive() on
866 * the queue, or while a thread may call
867 * \ref _mali_osk_notification_queue_receive() on the queue in the future.
869 * Therefore, all \ref _mali_osk_notification_t objects must be flushed and
870 * deleted by the code that makes use of the notification queues, since only
871 * they know the structure of the _mali_osk_notification_t::result_buffer
872 * (even if it may only be a flat sturcture).
874 * @note Since the queue is a FIFO, the code using notification queues may
875 * create its own 'flush' type of notification, to assist in flushing the
878 * Once the queue has been destroyed, it must not be used again.
880 * @param queue The queue to destroy
882 void _mali_osk_notification_queue_term(_mali_osk_notification_queue_t *queue);
884 /** @brief Schedule notification for delivery
886 * When a \ref _mali_osk_notification_t object has been created successfully
887 * and set up, it may be added to the queue of objects waiting for user space
890 * The sending will not block if the queue is full.
892 * A \ref _mali_osk_notification_t object \b must \b not be put on two different
893 * queues at the same time, or enqueued twice onto a single queue before
894 * reception. However, it is acceptable for it to be requeued \em after reception
895 * from a call to _mali_osk_notification_queue_receive(), even onto the same queue.
897 * Again, requeuing must also not enqueue onto two different queues at the same
898 * time, or enqueue onto the same queue twice before reception.
900 * @param queue The notification queue to add this notification to
901 * @param object The entry to add
903 void _mali_osk_notification_queue_send(_mali_osk_notification_queue_t *queue, _mali_osk_notification_t *object);
905 /** @brief Receive a notification from a queue
907 * Receives a single notification from the given queue.
909 * If no notifciations are ready the thread will sleep until one becomes ready.
910 * Therefore, notifications may not be received into an
911 * IRQ or 'atomic' context (that is, a context where sleeping is disallowed).
913 * @param queue The queue to receive from
914 * @param result Pointer to storage of a pointer of type
915 * \ref _mali_osk_notification_t*. \a result will be written to such that the
916 * expression \a (*result) will evaluate to a pointer to a valid
917 * \ref _mali_osk_notification_t object, or NULL if none were received.
918 * @return _MALI_OSK_ERR_OK on success. _MALI_OSK_ERR_RESTARTSYSCALL if the sleep was interrupted.
920 _mali_osk_errcode_t _mali_osk_notification_queue_receive(_mali_osk_notification_queue_t *queue, _mali_osk_notification_t **result);
922 /** @brief Dequeues a notification from a queue
924 * Receives a single notification from the given queue.
926 * If no notifciations are ready the function call will return an error code.
928 * @param queue The queue to receive from
929 * @param result Pointer to storage of a pointer of type
930 * \ref _mali_osk_notification_t*. \a result will be written to such that the
931 * expression \a (*result) will evaluate to a pointer to a valid
932 * \ref _mali_osk_notification_t object, or NULL if none were received.
933 * @return _MALI_OSK_ERR_OK on success, _MALI_OSK_ERR_ITEM_NOT_FOUND if queue was empty.
935 _mali_osk_errcode_t _mali_osk_notification_queue_dequeue(_mali_osk_notification_queue_t *queue, _mali_osk_notification_t **result);
937 /** @} */ /* end group _mali_osk_notification */
940 /** @addtogroup _mali_osk_timer
942 * Timers use the OS's representation of time, which are 'ticks'. This is to
943 * prevent aliasing problems between the internal timer time, and the time
948 /** @brief Initialize a timer
950 * Allocates resources for a new timer, and initializes them. This does not
953 * @return a pointer to the allocated timer object, or NULL on failure.
955 _mali_osk_timer_t *_mali_osk_timer_init(void);
957 /** @brief Start a timer
959 * It is an error to start a timer without setting the callback via
960 * _mali_osk_timer_setcallback().
962 * It is an error to use this to start an already started timer.
964 * The timer will expire in \a ticks_to_expire ticks, at which point, the
965 * callback function will be invoked with the callback-specific data,
966 * as registered by _mali_osk_timer_setcallback().
968 * @param tim the timer to start
969 * @param ticks_to_expire the amount of time in ticks for the timer to run
972 void _mali_osk_timer_add(_mali_osk_timer_t *tim, unsigned long ticks_to_expire);
974 /** @brief Modify a timer
976 * Set the relative time at which a timer will expire, and start it if it is
977 * stopped. If \a ticks_to_expire 0 the timer fires immediately.
979 * It is an error to modify a timer without setting the callback via
980 * _mali_osk_timer_setcallback().
982 * The timer will expire at \a ticks_to_expire from the time of the call, at
983 * which point, the callback function will be invoked with the
984 * callback-specific data, as set by _mali_osk_timer_setcallback().
986 * @param tim the timer to modify, and start if necessary
987 * @param ticks_to_expire the \em absolute time in ticks at which this timer
991 void _mali_osk_timer_mod(_mali_osk_timer_t *tim, unsigned long ticks_to_expire);
993 /** @brief Stop a timer, and block on its completion.
995 * Stop the timer. When the function returns, it is guaranteed that the timer's
996 * callback will not be running on any CPU core.
998 * Since stoping the timer blocks on compeletion of the callback, the callback
999 * may not obtain any mutexes that the caller holds. Otherwise, a deadlock will
1002 * @note While the callback itself is guaranteed to not be running, work
1003 * enqueued on the work-queue by the timer (with
1004 * \ref _mali_osk_wq_schedule_work()) may still run. The timer callback and
1005 * work handler must take this into account.
1007 * It is legal to stop an already stopped timer.
1009 * @param tim the timer to stop.
1012 void _mali_osk_timer_del(_mali_osk_timer_t *tim);
1014 /** @brief Stop a timer.
1016 * Stop the timer. When the function returns, the timer's callback may still be
1017 * running on any CPU core.
1019 * It is legal to stop an already stopped timer.
1021 * @param tim the timer to stop.
1023 void _mali_osk_timer_del_async(_mali_osk_timer_t *tim);
1025 /** @brief Check if timer is pending.
1027 * Check if timer is active.
1029 * @param tim the timer to check
1030 * @return MALI_TRUE if time is active, MALI_FALSE if it is not active
1032 mali_bool _mali_osk_timer_pending(_mali_osk_timer_t *tim);
1034 /** @brief Set a timer's callback parameters.
1036 * This must be called at least once before a timer is started/modified.
1038 * After a timer has been stopped or expires, the callback remains set. This
1039 * means that restarting the timer will call the same function with the same
1040 * parameters on expiry.
1042 * @param tim the timer to set callback on.
1043 * @param callback Function to call when timer expires
1044 * @param data Function-specific data to supply to the function on expiry.
1046 void _mali_osk_timer_setcallback(_mali_osk_timer_t *tim, _mali_osk_timer_callback_t callback, void *data);
1048 /** @brief Terminate a timer, and deallocate resources.
1050 * The timer must first be stopped by calling _mali_osk_timer_del().
1052 * It is a programming error for _mali_osk_timer_term() to be called on:
1053 * - timer that is currently running
1054 * - a timer that is currently executing its callback.
1056 * @param tim the timer to deallocate.
1058 void _mali_osk_timer_term(_mali_osk_timer_t *tim);
1059 /** @} */ /* end group _mali_osk_timer */
1062 /** @defgroup _mali_osk_time OSK Time functions
1064 * \ref _mali_osk_time use the OS's representation of time, which are
1065 * 'ticks'. This is to prevent aliasing problems between the internal timer
1066 * time, and the time asked for.
1068 * OS tick time is measured as a u32. The time stored in a u32 may either be
1069 * an absolute time, or a time delta between two events. Whilst it is valid to
1070 * use math opeartors to \em change the tick value represented as a u32, it
1071 * is often only meaningful to do such operations on time deltas, rather than
1072 * on absolute time. However, it is meaningful to add/subtract time deltas to
1075 * Conversion between tick time and milliseconds (ms) may not be loss-less,
1076 * and are \em implementation \em depenedant.
1078 * Code use OS time must take this into account, since:
1079 * - a small OS time may (or may not) be rounded
1080 * - a large time may (or may not) overflow
1084 /** @brief Return whether ticka occurs after or at the same time as tickb
1086 * Systems where ticks can wrap must handle that.
1088 * @param ticka ticka
1089 * @param tickb tickb
1090 * @return MALI_TRUE if ticka represents a time that occurs at or after tickb.
1092 mali_bool _mali_osk_time_after_eq(unsigned long ticka, unsigned long tickb);
1094 /** @brief Convert milliseconds to OS 'ticks'
1096 * @param ms time interval in milliseconds
1097 * @return the corresponding time interval in OS ticks.
1099 unsigned long _mali_osk_time_mstoticks(u32 ms);
1101 /** @brief Convert OS 'ticks' to milliseconds
1103 * @param ticks time interval in OS ticks.
1104 * @return the corresponding time interval in milliseconds
1106 u32 _mali_osk_time_tickstoms(unsigned long ticks);
1109 /** @brief Get the current time in OS 'ticks'.
1110 * @return the current time in OS 'ticks'.
1112 unsigned long _mali_osk_time_tickcount(void);
1114 /** @brief Cause a microsecond delay
1116 * The delay will have microsecond resolution, and is necessary for correct
1117 * operation of the driver. At worst, the delay will be \b at least \a usecs
1118 * microseconds, and so may be (significantly) more.
1120 * This function may be implemented as a busy-wait, which is the most sensible
1121 * implementation. On OSs where there are situations in which a thread must not
1122 * sleep, this is definitely implemented as a busy-wait.
1124 * @param usecs the number of microseconds to wait for.
1126 void _mali_osk_time_ubusydelay(u32 usecs);
1128 /** @brief Return time in nano seconds, since any given reference.
1130 * @return Time in nano seconds
1132 u64 _mali_osk_time_get_ns(void);
1134 /** @brief Return time in nano seconds, since boot time.
1136 * @return Time in nano seconds
1138 u64 _mali_osk_boot_time_get_ns(void);
1140 /** @} */ /* end group _mali_osk_time */
1142 /** @defgroup _mali_osk_math OSK Math
1145 /** @brief Count Leading Zeros (Little-endian)
1147 * @note This function must be implemented to support the reference
1148 * implementation of _mali_osk_find_first_zero_bit, as defined in
1149 * mali_osk_bitops.h.
1151 * @param val 32-bit words to count leading zeros on
1152 * @return the number of leading zeros.
1154 u32 _mali_osk_clz(u32 val);
1156 /** @brief find last (most-significant) bit set
1158 * @param val 32-bit words to count last bit set on
1159 * @return last bit set.
1161 u32 _mali_osk_fls(u32 val);
1163 /** @} */ /* end group _mali_osk_math */
1165 /** @addtogroup _mali_osk_wait_queue OSK Wait Queue functionality
1168 /** @brief Initialize an empty Wait Queue */
1169 _mali_osk_wait_queue_t *_mali_osk_wait_queue_init(void);
1171 /** @brief Sleep if condition is false
1173 * @param queue the queue to use
1174 * @param condition function pointer to a boolean function
1175 * @param data data parameter for condition function
1177 * Put thread to sleep if the given \a condition function returns false. When
1178 * being asked to wake up again, the condition will be re-checked and the
1179 * thread only woken up if the condition is now true.
1181 void _mali_osk_wait_queue_wait_event(_mali_osk_wait_queue_t *queue, mali_bool(*condition)(void *), void *data);
1183 /** @brief Sleep if condition is false
1185 * @param queue the queue to use
1186 * @param condition function pointer to a boolean function
1187 * @param data data parameter for condition function
1188 * @param timeout timeout in ms
1190 * Put thread to sleep if the given \a condition function returns false. When
1191 * being asked to wake up again, the condition will be re-checked and the
1192 * thread only woken up if the condition is now true. Will return if time
1195 void _mali_osk_wait_queue_wait_event_timeout(_mali_osk_wait_queue_t *queue, mali_bool(*condition)(void *), void *data, u32 timeout);
1197 /** @brief Wake up all threads in wait queue if their respective conditions are
1200 * @param queue the queue whose threads should be woken up
1202 * Wake up all threads in wait queue \a queue whose condition is now true.
1204 void _mali_osk_wait_queue_wake_up(_mali_osk_wait_queue_t *queue);
1206 /** @brief terminate a wait queue
1208 * @param queue the queue to terminate.
1210 void _mali_osk_wait_queue_term(_mali_osk_wait_queue_t *queue);
1211 /** @} */ /* end group _mali_osk_wait_queue */
1214 /** @addtogroup _mali_osk_miscellaneous
1217 /** @brief Output a device driver debug message.
1219 * The interpretation of \a fmt is the same as the \c format parameter in
1220 * _mali_osu_vsnprintf().
1222 * @param fmt a _mali_osu_vsnprintf() style format string
1223 * @param ... a variable-number of parameters suitable for \a fmt
1225 void _mali_osk_dbgmsg(const char *fmt, ...);
1227 /** @brief Print fmt into buf.
1229 * The interpretation of \a fmt is the same as the \c format parameter in
1230 * _mali_osu_vsnprintf().
1232 * @param buf a pointer to the result buffer
1233 * @param size the total number of bytes allowed to write to \a buf
1234 * @param fmt a _mali_osu_vsnprintf() style format string
1235 * @param ... a variable-number of parameters suitable for \a fmt
1236 * @return The number of bytes written to \a buf
1238 u32 _mali_osk_snprintf(char *buf, u32 size, const char *fmt, ...);
1240 /** @brief Print fmt into print_ctx.
1242 * The interpretation of \a fmt is the same as the \c format parameter in
1243 * _mali_osu_vsnprintf().
1245 * @param print_ctx a pointer to the result file buffer
1246 * @param fmt a _mali_osu_vsnprintf() style format string
1247 * @param ... a variable-number of parameters suitable for \a fmt
1249 void _mali_osk_ctxprintf(_mali_osk_print_ctx *print_ctx, const char *fmt, ...);
1251 /** @brief Abnormal process abort.
1253 * Terminates the caller-process if this function is called.
1255 * This function will be called from Debug assert-macros in mali_kernel_common.h.
1257 * This function will never return - because to continue from a Debug assert
1258 * could cause even more problems, and hinder debugging of the initial problem.
1260 * This function is only used in Debug builds, and is not used in Release builds.
1262 void _mali_osk_abort(void);
1264 /** @brief Sets breakpoint at point where function is called.
1266 * This function will be called from Debug assert-macros in mali_kernel_common.h,
1267 * to assist in debugging. If debugging at this level is not required, then this
1268 * function may be implemented as a stub.
1270 * This function is only used in Debug builds, and is not used in Release builds.
1272 void _mali_osk_break(void);
1274 /** @brief Return an identificator for calling process.
1276 * @return Identificator for calling process.
1278 u32 _mali_osk_get_pid(void);
1280 /** @brief Return an name for calling process.
1282 * @return name for calling process.
1284 char *_mali_osk_get_comm(void);
1286 /** @brief Return an identificator for calling thread.
1288 * @return Identificator for calling thread.
1290 u32 _mali_osk_get_tid(void);
1293 /** @brief Take a reference to the power manager system for the Mali device (synchronously).
1295 * When function returns successfully, Mali is ON.
1297 * @note Call \a _mali_osk_pm_dev_ref_put() to release this reference.
1299 _mali_osk_errcode_t _mali_osk_pm_dev_ref_get_sync(void);
1301 /** @brief Take a reference to the external power manager system for the Mali device (asynchronously).
1303 * Mali might not yet be on after this function as returned.
1304 * Please use \a _mali_osk_pm_dev_barrier() or \a _mali_osk_pm_dev_ref_get_sync()
1305 * to wait for Mali to be powered on.
1307 * @note Call \a _mali_osk_pm_dev_ref_dec() to release this reference.
1309 _mali_osk_errcode_t _mali_osk_pm_dev_ref_get_async(void);
1311 /** @brief Release the reference to the external power manger system for the Mali device.
1313 * When reference count reach zero, the cores can be off.
1315 * @note This must be used to release references taken with
1316 * \a _mali_osk_pm_dev_ref_get_sync() or \a _mali_osk_pm_dev_ref_get_sync().
1318 void _mali_osk_pm_dev_ref_put(void);
1320 /** @brief Block until pending PM operations are done
1322 void _mali_osk_pm_dev_barrier(void);
1324 /** @} */ /* end group _mali_osk_miscellaneous */
1326 /** @} */ /* end group osuapi */
1328 /** @} */ /* end group uddapi */
1336 /* Check standard inlines */
1337 #ifndef MALI_STATIC_INLINE
1338 #error MALI_STATIC_INLINE not defined on your OS
1341 #ifndef MALI_NON_STATIC_INLINE
1342 #error MALI_NON_STATIC_INLINE not defined on your OS
1345 #endif /* __MALI_OSK_H__ */