Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.15 KB | None | 0 0
  1. #ifndef LIBDRIVER_H
  2. #define LIBDRIVER_H
  3.  
  4. #include <stdint.h>
  5. #include <stddef.h>
  6.  
  7. /*
  8.  * Note:    any functions not marked as weak might not be implemented by the host, in that
  9.  *          case the driver may choose to either try and continue without them, or just fail
  10.  */
  11.  
  12. ///////////////////////////////////////////////////////////////////////////
  13. // Memory Allocation
  14. ///////////////////////////////////////////////////////////////////////////
  15.  
  16. /**
  17.  * The addr represents a physical address to allocate from
  18.  *
  19.  * @remark
  20.  * This implies LIBDRIVER_FLAG_CONTIGUOUS
  21.  */
  22. #define LIBDRIVER_FLAG_PHYS         (1 << 0)
  23.  
  24. /**
  25.  * The memory allocated should be physically contiguous
  26.  */
  27. #define LIBDRIVER_FLAG_CONTIGUOUS   (1 << 1)
  28.  
  29. /**
  30.  * Map memory for the driver
  31.  *
  32.  * @remark
  33.  * The driver can assume the given memory is READ/WRITE but not EXEC
  34.  *
  35.  * @param addr      [IN] Used with LIBDRIVER_FLAG_PHYS
  36.  * @param length    [IN] The length of memory to map
  37.  * @param flags     [IN] Flags about how to allocate the memory
  38.  */
  39. void* libdriver_map(uintptr_t addr, size_t length, int flags);
  40.  
  41. /**
  42.  * Unmap mapped memory
  43.  *
  44.  * @param addr      [IN]    The virtual address to unmap from
  45.  * @param length    [IN]    The length to unmap
  46.  */
  47. int libdriver_unmap(void* addr, size_t length);
  48.  
  49. /**
  50.  * Will convert the given virtual address to a physical address
  51.  *
  52.  * @param addr      [IN] The virtual address
  53.  */
  54. void* libdriver_virt_to_phys(uintptr_t addr);
  55.  
  56. ///////////////////////////////////////////////////////////////////////////
  57. // IO
  58. ///////////////////////////////////////////////////////////////////////////
  59.  
  60. /**
  61.  * PORT IO operations
  62.  */
  63. void libdriver_outb(uint16_t port, uint8_t data);
  64. void libdriver_outw(uint16_t port, uint16_t data);
  65. void libdriver_outd(uint16_t port, uint32_t data);
  66. uint8_t libdriver_inb(uint16_t port);
  67. uint16_t libdriver_inw(uint16_t port);
  68. uint32_t libdriver_ind(uint16_t port);
  69.  
  70. /**
  71.  * PCI operations
  72.  */
  73. void libdriver_pci_writeb(uint16_t segment, uint8_t bus, uint8_t device, uint8_t function, uint16_t offset, uint8_t data);
  74. uint8_t libdriver_pci_readb(uint16_t segment, uint8_t bus, uint8_t device, uint8_t function, uint16_t offset);
  75. void libdriver_pci_writew(uint16_t segment, uint8_t bus, uint8_t device, uint8_t function, uint16_t offset, uint16_t data);
  76. uint16_t libdriver_pci_readw(uint16_t segment, uint8_t bus, uint8_t device, uint8_t function, uint16_t offset);
  77. void libdriver_pci_writed(uint16_t segment, uint8_t bus, uint8_t device, uint8_t function, uint16_t offset, uint32_t data);
  78. uint32_t libdriver_pci_readd(uint16_t segment, uint8_t bus, uint8_t device, uint8_t function, uint16_t offset);
  79.  
  80. #define LIBDRIVER_INVALID_IRQ (-1)
  81.  
  82. /**
  83.  * Get the pci irq from the given pci device
  84.  *
  85.  * This may return LIBDRIVER_INVALID_IRQ if there was a problem routing the irq
  86.  */
  87. __attribute__((weak)) int libdriver_pci_irq_route(uint16_t segment, uint8_t bus, uint8_t device, uint8_t function);
  88.  
  89. ///////////////////////////////////////////////////////////////////////////
  90. // Interrupt handling
  91. ///////////////////////////////////////////////////////////////////////////
  92.  
  93. typedef void(*libdriver_function_t)(void* context);
  94.  
  95. /**
  96.  * Register an interrupt handler
  97.  *
  98.  * The interrupt is an GSI, so if working in apic mode it has to be redirected in the APIC
  99.  *
  100.  * @remark
  101.  * Passing LIBDRIVER_INVALID_IRQ will do nothing
  102.  *
  103.  * @remark
  104.  * The driver can assume that all the mapped and allocated memory from the libdriver functions are
  105.  * still accessible from the interrupt
  106.  *
  107.  * @param irq       [IN] The interrupt to register
  108.  * @param handler   [IN] The handler to call
  109.  * @param user      [IN] The data to pass to the interrupt handler
  110.  */
  111. __attribute__((weak)) void libdriver_request_interrupt(int irq, libdriver_function_t handler, void* user);
  112.  
  113. /**
  114.  * Request from the host an address and data for an msi, with the handler to call on that interrupt
  115.  *
  116.  * The idea is to allow the driver to use MSI/MSI-X but for the host to still manage the interrupt themselves
  117.  *
  118.  * @remark
  119.  * The driver can assume that all the mapped and allocated memory from the libdriver functions are
  120.  * still accessible from the interrupt
  121.  *
  122.  * @param msi_addr      [OUT]   The address to write in the msi
  123.  * @param msi_data      [OUT]   The data to write in the msi
  124.  * @param handler       [IN]    The handler to call on interrupt
  125.  * @param user          [IN]    The data to pass to the interrupt handler
  126.  */
  127. __attribute__((weak)) void libdriver_request_msi(uint64_t* msi_addr, uint32_t* msi_data, libdriver_function_t handler, void* user);
  128.  
  129. ///////////////////////////////////////////////////////////////////////////
  130. // Threading
  131. ///////////////////////////////////////////////////////////////////////////
  132.  
  133. /*
  134.  * Create a new thread
  135.  *
  136.  * The device should always call kill thread at the end of the handler
  137.  *
  138.  * @param handler   [IN]    The entry of the thread
  139.  * @param user      [OUT]   The data to call the entry with
  140.  */
  141. __attribute__((weak)) void* libdriver_create_thread(libdriver_function_t handler, void* user);
  142.  
  143. /**
  144.  * Yield from the current thread
  145.  */
  146. __attribute__((weak)) void libdriver_thread_yield();
  147.  
  148. /**
  149.  * Kill the thread using the value returned on the thread creation
  150.  */
  151. __attribute__((weak)) void libdriver_kill_thread(void* thread);
  152.  
  153. ///////////////////////////////////////////////////////////////////////////
  154. // Logging related
  155. ///////////////////////////////////////////////////////////////////////////
  156.  
  157. /**
  158.  * Something really bad that the driver can not correct itself from has
  159.  * happened, assume the driver will not work anymore
  160.  */
  161. #define LIBDRIVER_LOG_FATAL     (4)
  162.  
  163. /**
  164.  * Something bad happened but the driver can still work, but might have
  165.  * some side affects on the operations
  166.  */
  167. #define LIBDRIVER_LOG_ERROR     (3)
  168.  
  169. /**
  170.  * Something happened which was unexpected but is fine
  171.  */
  172. #define LIBDRIVER_LOG_WARN      (2)
  173.  
  174. /**
  175.  * Debug messages, usually used to diagnose driver problems
  176.  */
  177. #define LIBDRIVER_LOG_DEBUG     (1)
  178.  
  179. /**
  180.  * Allows the driver to log stuff, the host may choose to not implement this function
  181.  */
  182. __attribute__((weak)) void libdriver_log(int level, const char* message);
  183.  
  184. #endif //LIBDRIVER_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement