1 /*
2 * Linux OS Independent Layer
3 *
4 * Copyright (C) 2020, Broadcom.
5 *
6 * Unless you and Broadcom execute a separate written software license
7 * agreement governing use of this software, this software is licensed to you
8 * under the terms of the GNU General Public License version 2 (the "GPL"),
9 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
10 * following added to such license:
11 *
12 * As a special exception, the copyright holders of this software give you
13 * permission to link this software with independent modules, and to copy and
14 * distribute the resulting executable under terms of your choice, provided that
15 * you also meet, for each linked independent module, the terms and conditions of
16 * the license of that module. An independent module is a module which is not
17 * derived from this software. The special exception does not apply to any
18 * modifications of the software.
19 *
20 *
21 * <<Broadcom-WL-IPTag/Dual:>>
22 */
23
24 #define LINUX_PORT
25
26 #include <typedefs.h>
27 #include <bcmendian.h>
28 #include <linuxver.h>
29 #include <bcmdefs.h>
30
31 #if defined(__ARM_ARCH_7A__) && !defined(DHD_USE_COHERENT_MEM_FOR_RING)
32 #include <asm/cacheflush.h>
33 #endif /* __ARM_ARCH_7A__ && !DHD_USE_COHERENT_MEM_FOR_RING */
34
35 #include <linux/random.h>
36
37 #include <osl.h>
38 #include <bcmutils.h>
39 #include <linux/delay.h>
40 #include <linux/vmalloc.h>
41 #include <pcicfg.h>
42 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(4, 8, 0))
43 #include <asm-generic/pci-dma-compat.h>
44 #endif
45
46 #if defined(BCMASSERT_LOG) && !defined(OEM_ANDROID)
47 #include <bcm_assert_log.h>
48 #endif
49
50 #include <linux/fs.h>
51
52 #ifdef BCM_OBJECT_TRACE
53 #include <bcmutils.h>
54 #endif /* BCM_OBJECT_TRACE */
55 #include "linux_osl_priv.h"
56
57 #define PCI_CFG_RETRY 10 /* PR15065: retry count for pci cfg accesses */
58
59 #define DUMPBUFSZ 1024
60
61 #ifdef CUSTOMER_HW4_DEBUG
62 uint32 g_assert_type = 1; /* By Default not cause Kernel Panic */
63 #else
64 uint32 g_assert_type = 0; /* By Default Kernel Panic */
65 #endif /* CUSTOMER_HW4_DEBUG */
66
67 module_param(g_assert_type, int, 0);
68
69 #if defined(BCMSLTGT)
70 /* !!!make sure htclkratio is not 0!!! */
71 extern uint htclkratio;
72 #endif
73
74 #ifdef USE_DMA_LOCK
75 static void osl_dma_lock(osl_t *osh);
76 static void osl_dma_unlock(osl_t *osh);
77 static void osl_dma_lock_init(osl_t *osh);
78
79 #define DMA_LOCK(osh) osl_dma_lock(osh)
80 #define DMA_UNLOCK(osh) osl_dma_unlock(osh)
81 #define DMA_LOCK_INIT(osh) osl_dma_lock_init(osh);
82 #else
83 #define DMA_LOCK(osh) do { /* noop */ } while(0)
84 #define DMA_UNLOCK(osh) do { /* noop */ } while(0)
85 #define DMA_LOCK_INIT(osh) do { /* noop */ } while(0)
86 #endif /* USE_DMA_LOCK */
87
88 static int16 linuxbcmerrormap[] =
89 { 0, /* 0 */
90 -EINVAL, /* BCME_ERROR */
91 -EINVAL, /* BCME_BADARG */
92 -EINVAL, /* BCME_BADOPTION */
93 -EINVAL, /* BCME_NOTUP */
94 -EINVAL, /* BCME_NOTDOWN */
95 -EINVAL, /* BCME_NOTAP */
96 -EINVAL, /* BCME_NOTSTA */
97 -EINVAL, /* BCME_BADKEYIDX */
98 -EINVAL, /* BCME_RADIOOFF */
99 -EINVAL, /* BCME_NOTBANDLOCKED */
100 -EINVAL, /* BCME_NOCLK */
101 -EINVAL, /* BCME_BADRATESET */
102 -EINVAL, /* BCME_BADBAND */
103 -E2BIG, /* BCME_BUFTOOSHORT */
104 -E2BIG, /* BCME_BUFTOOLONG */
105 -EBUSY, /* BCME_BUSY */
106 -EINVAL, /* BCME_NOTASSOCIATED */
107 -EINVAL, /* BCME_BADSSIDLEN */
108 -EINVAL, /* BCME_OUTOFRANGECHAN */
109 -EINVAL, /* BCME_BADCHAN */
110 -EFAULT, /* BCME_BADADDR */
111 -ENOMEM, /* BCME_NORESOURCE */
112 -EOPNOTSUPP, /* BCME_UNSUPPORTED */
113 -EMSGSIZE, /* BCME_BADLENGTH */
114 -EINVAL, /* BCME_NOTREADY */
115 -EPERM, /* BCME_EPERM */
116 -ENOMEM, /* BCME_NOMEM */
117 -EINVAL, /* BCME_ASSOCIATED */
118 -ERANGE, /* BCME_RANGE */
119 -EINVAL, /* BCME_NOTFOUND */
120 -EINVAL, /* BCME_WME_NOT_ENABLED */
121 -EINVAL, /* BCME_TSPEC_NOTFOUND */
122 -EINVAL, /* BCME_ACM_NOTSUPPORTED */
123 -EINVAL, /* BCME_NOT_WME_ASSOCIATION */
124 -EIO, /* BCME_SDIO_ERROR */
125 -ENODEV, /* BCME_DONGLE_DOWN */
126 -EINVAL, /* BCME_VERSION */
127 -EIO, /* BCME_TXFAIL */
128 -EIO, /* BCME_RXFAIL */
129 -ENODEV, /* BCME_NODEVICE */
130 -EINVAL, /* BCME_NMODE_DISABLED */
131 -ENODATA, /* BCME_NONRESIDENT */
132 -EINVAL, /* BCME_SCANREJECT */
133 -EINVAL, /* BCME_USAGE_ERROR */
134 -EIO, /* BCME_IOCTL_ERROR */
135 -EIO, /* BCME_SERIAL_PORT_ERR */
136 -EOPNOTSUPP, /* BCME_DISABLED, BCME_NOTENABLED */
137 -EIO, /* BCME_DECERR */
138 -EIO, /* BCME_ENCERR */
139 -EIO, /* BCME_MICERR */
140 -ERANGE, /* BCME_REPLAY */
141 -EINVAL, /* BCME_IE_NOTFOUND */
142 -EINVAL, /* BCME_DATA_NOTFOUND */
143 -EINVAL, /* BCME_NOT_GC */
144 -EINVAL, /* BCME_PRS_REQ_FAILED */
145 -EINVAL, /* BCME_NO_P2P_SE */
146 -EINVAL, /* BCME_NOA_PND */
147 -EINVAL, /* BCME_FRAG_Q_FAILED */
148 -EINVAL, /* BCME_GET_AF_FAILED */
149 -EINVAL, /* BCME_MSCH_NOTREADY */
150 -EINVAL, /* BCME_IOV_LAST_CMD */
151 -EINVAL, /* BCME_MINIPMU_CAL_FAIL */
152 -EINVAL, /* BCME_RCAL_FAIL */
153 -EINVAL, /* BCME_LPF_RCCAL_FAIL */
154 -EINVAL, /* BCME_DACBUF_RCCAL_FAIL */
155 -EINVAL, /* BCME_VCOCAL_FAIL */
156 -EINVAL, /* BCME_BANDLOCKED */
157 -EINVAL, /* BCME_BAD_IE_DATA */
158 -EINVAL, /* BCME_REG_FAILED */
159 -EINVAL, /* BCME_NOCHAN */
160 -EINVAL, /* BCME_PKTTOSS */
161 -EINVAL, /* BCME_DNGL_DEVRESET */
162 -EINVAL, /* BCME_ROAM */
163 -EOPNOTSUPP, /* BCME_NO_SIG_FILE */
164
165 /* When an new error code is added to bcmutils.h, add os
166 * specific error translation here as well
167 */
168 /* check if BCME_LAST changed since the last time this function was updated */
169 #if BCME_LAST != BCME_NO_SIG_FILE
170 #error "You need to add a OS error translation in the linuxbcmerrormap \
171 for new error code defined in bcmutils.h"
172 #endif
173 };
174 uint lmtest = FALSE;
175
176 #ifdef DHD_MAP_LOGGING
177 #define DHD_MAP_LOG_SIZE 2048
178
179 typedef struct dhd_map_item {
180 dmaaddr_t pa; /* DMA address (physical) */
181 uint64 ts_nsec; /* timestamp: nsec */
182 uint32 size; /* mapping size */
183 uint8 rsvd[4]; /* reserved for future use */
184 } dhd_map_item_t;
185
186 typedef struct dhd_map_record {
187 uint32 items; /* number of total items */
188 uint32 idx; /* current index of metadata */
189 dhd_map_item_t map[0]; /* metadata storage */
190 } dhd_map_log_t;
191
192 void
osl_dma_map_dump(osl_t * osh)193 osl_dma_map_dump(osl_t *osh)
194 {
195 dhd_map_log_t *map_log, *unmap_log;
196 uint64 ts_sec, ts_usec;
197
198 map_log = (dhd_map_log_t *)(osh->dhd_map_log);
199 unmap_log = (dhd_map_log_t *)(osh->dhd_unmap_log);
200 osl_get_localtime(&ts_sec, &ts_usec);
201
202 if (map_log && unmap_log) {
203 printk("%s: map_idx=%d unmap_idx=%d "
204 "current time=[%5lu.%06lu]\n", __FUNCTION__,
205 map_log->idx, unmap_log->idx, (unsigned long)ts_sec,
206 (unsigned long)ts_usec);
207 printk("%s: dhd_map_log(pa)=0x%llx size=%d,"
208 " dma_unmap_log(pa)=0x%llx size=%d\n", __FUNCTION__,
209 (uint64)__virt_to_phys((ulong)(map_log->map)),
210 (uint32)(sizeof(dhd_map_item_t) * map_log->items),
211 (uint64)__virt_to_phys((ulong)(unmap_log->map)),
212 (uint32)(sizeof(dhd_map_item_t) * unmap_log->items));
213 }
214 }
215
216 static void *
osl_dma_map_log_init(uint32 item_len)217 osl_dma_map_log_init(uint32 item_len)
218 {
219 dhd_map_log_t *map_log;
220 gfp_t flags;
221 uint32 alloc_size = (uint32)(sizeof(dhd_map_log_t) +
222 (item_len * sizeof(dhd_map_item_t)));
223
224 flags = CAN_SLEEP() ? GFP_KERNEL : GFP_ATOMIC;
225 map_log = (dhd_map_log_t *)kmalloc(alloc_size, flags);
226 if (map_log) {
227 memset(map_log, 0, alloc_size);
228 map_log->items = item_len;
229 map_log->idx = 0;
230 }
231
232 return (void *)map_log;
233 }
234
235 static void
osl_dma_map_log_deinit(osl_t * osh)236 osl_dma_map_log_deinit(osl_t *osh)
237 {
238 if (osh->dhd_map_log) {
239 kfree(osh->dhd_map_log);
240 osh->dhd_map_log = NULL;
241 }
242
243 if (osh->dhd_unmap_log) {
244 kfree(osh->dhd_unmap_log);
245 osh->dhd_unmap_log = NULL;
246 }
247 }
248
249 static void
osl_dma_map_logging(osl_t * osh,void * handle,dmaaddr_t pa,uint32 len)250 osl_dma_map_logging(osl_t *osh, void *handle, dmaaddr_t pa, uint32 len)
251 {
252 dhd_map_log_t *log = (dhd_map_log_t *)handle;
253 uint32 idx;
254
255 if (log == NULL) {
256 printk("%s: log is NULL\n", __FUNCTION__);
257 return;
258 }
259
260 idx = log->idx;
261 log->map[idx].ts_nsec = osl_localtime_ns();
262 log->map[idx].pa = pa;
263 log->map[idx].size = len;
264 log->idx = (idx + 1) % log->items;
265 }
266 #endif /* DHD_MAP_LOGGING */
267
268 /* translate bcmerrors into linux errors */
269 int
osl_error(int bcmerror)270 osl_error(int bcmerror)
271 {
272 if (bcmerror > 0)
273 bcmerror = 0;
274 else if (bcmerror < BCME_LAST)
275 bcmerror = BCME_ERROR;
276
277 /* Array bounds covered by ASSERT in osl_attach */
278 return linuxbcmerrormap[-bcmerror];
279 }
280 #ifdef SHARED_OSL_CMN
281 osl_t *
osl_attach(void * pdev,uint bustype,bool pkttag,void ** osl_cmn)282 osl_attach(void *pdev, uint bustype, bool pkttag, void **osl_cmn)
283 {
284 #else
285 osl_t *
286 osl_attach(void *pdev, uint bustype, bool pkttag)
287 {
288 void **osl_cmn = NULL;
289 #endif /* SHARED_OSL_CMN */
290 osl_t *osh;
291 gfp_t flags;
292
293 flags = CAN_SLEEP() ? GFP_KERNEL: GFP_ATOMIC;
294 if (!(osh = kmalloc(sizeof(osl_t), flags)))
295 return osh;
296
297 ASSERT(osh);
298
299 bzero(osh, sizeof(osl_t));
300
301 if (osl_cmn == NULL || *osl_cmn == NULL) {
302 if (!(osh->cmn = kmalloc(sizeof(osl_cmn_t), flags))) {
303 kfree(osh);
304 return NULL;
305 }
306 bzero(osh->cmn, sizeof(osl_cmn_t));
307 if (osl_cmn)
308 *osl_cmn = osh->cmn;
309 atomic_set(&osh->cmn->malloced, 0);
310 osh->cmn->dbgmem_list = NULL;
311 spin_lock_init(&(osh->cmn->dbgmem_lock));
312
313 #ifdef BCMDBG_PKT
314 spin_lock_init(&(osh->cmn->pktlist_lock));
315 #endif
316 spin_lock_init(&(osh->cmn->pktalloc_lock));
317
318 } else {
319 osh->cmn = *osl_cmn;
320 }
321 atomic_add(1, &osh->cmn->refcount);
322
323 bcm_object_trace_init();
324 /* Check that error map has the right number of entries in it */
325 ASSERT(ABS(BCME_LAST) == (ARRAYSIZE(linuxbcmerrormap) - 1));
326 osh->failed = 0;
327 osh->pdev = pdev;
328 osh->pub.pkttag = pkttag;
329 osh->bustype = bustype;
330 osh->magic = OS_HANDLE_MAGIC;
331
332 switch (bustype) {
333 case PCI_BUS:
334 case SI_BUS:
335 osh->pub.mmbus = TRUE;
336 break;
337 case SDIO_BUS:
338 case USB_BUS:
339 case SPI_BUS:
340 case RPC_BUS:
341 osh->pub.mmbus = FALSE;
342 break;
343 default:
344 ASSERT(FALSE);
345 break;
346 }
347
348 #ifdef BCMDBG_CTRACE
349 spin_lock_init(&osh->ctrace_lock);
350 INIT_LIST_HEAD(&osh->ctrace_list);
351 osh->ctrace_num = 0;
352 #endif /* BCMDBG_CTRACE */
353
354 DMA_LOCK_INIT(osh);
355
356 #ifdef BCMDBG_ASSERT
357 if (pkttag) {
358 struct sk_buff *skb;
359 BCM_REFERENCE(skb);
360 ASSERT(OSL_PKTTAG_SZ <= sizeof(skb->cb));
361 }
362 #endif
363
364 #ifdef DHD_MAP_LOGGING
365 osh->dhd_map_log = osl_dma_map_log_init(DHD_MAP_LOG_SIZE);
366 if (osh->dhd_map_log == NULL) {
367 printk("%s: Failed to alloc dhd_map_log\n", __FUNCTION__);
368 }
369
370 osh->dhd_unmap_log = osl_dma_map_log_init(DHD_MAP_LOG_SIZE);
371 if (osh->dhd_unmap_log == NULL) {
372 printk("%s: Failed to alloc dhd_unmap_log\n", __FUNCTION__);
373 }
374 #endif /* DHD_MAP_LOGGING */
375
376 return osh;
377 }
378
379 void osl_set_bus_handle(osl_t *osh, void *bus_handle)
380 {
381 osh->bus_handle = bus_handle;
382 }
383
384 void* osl_get_bus_handle(osl_t *osh)
385 {
386 return osh->bus_handle;
387 }
388
389 #if defined(AXI_TIMEOUTS_NIC)
390 void osl_set_bpt_cb(osl_t *osh, void *bpt_cb, void *bpt_ctx)
391 {
392 if (osh) {
393 osh->bpt_cb = (bpt_cb_fn)bpt_cb;
394 osh->sih = bpt_ctx;
395 }
396 }
397 #endif /* AXI_TIMEOUTS_NIC */
398
399 void
400 osl_detach(osl_t *osh)
401 {
402 if (osh == NULL)
403 return;
404
405 #ifdef BCMDBG_MEM
406 if (MEMORY_LEFTOVER(osh)) {
407 static char dumpbuf[DUMPBUFSZ];
408 struct bcmstrbuf b;
409
410 printf("%s: MEMORY LEAK %d bytes\n", __FUNCTION__, MALLOCED(osh));
411 bcm_binit(&b, dumpbuf, DUMPBUFSZ);
412 MALLOC_DUMP(osh, &b);
413 printf("%s", b.origbuf);
414 }
415 #endif
416
417 bcm_object_trace_deinit();
418
419 #ifdef DHD_MAP_LOGGING
420 osl_dma_map_log_deinit(osh);
421 #endif /* DHD_MAP_LOGGING */
422
423 ASSERT(osh->magic == OS_HANDLE_MAGIC);
424 atomic_sub(1, &osh->cmn->refcount);
425 if (atomic_read(&osh->cmn->refcount) == 0) {
426 kfree(osh->cmn);
427 }
428 kfree(osh);
429 }
430
431 /* APIs to set/get specific quirks in OSL layer */
432 void
433 BCMFASTPATH(osl_flag_set)(osl_t *osh, uint32 mask)
434 {
435 osh->flags |= mask;
436 }
437
438 void
439 osl_flag_clr(osl_t *osh, uint32 mask)
440 {
441 osh->flags &= ~mask;
442 }
443
444 bool
445 osl_is_flag_set(osl_t *osh, uint32 mask)
446 {
447 return (osh->flags & mask);
448 }
449
450 #if (defined(__ARM_ARCH_7A__) && !defined(DHD_USE_COHERENT_MEM_FOR_RING))
451
452 inline void
453 BCMFASTPATH(osl_cache_flush)(void *va, uint size)
454 {
455 if (size > 0)
456 dma_sync_single_for_device(OSH_NULL, virt_to_dma(OSH_NULL, va), size,
457 DMA_TO_DEVICE);
458 }
459
460 inline void
461 BCMFASTPATH(osl_cache_inv)(void *va, uint size)
462 {
463 dma_sync_single_for_cpu(OSH_NULL, virt_to_dma(OSH_NULL, va), size, DMA_FROM_DEVICE);
464 }
465
466 inline void
467 BCMFASTPATH(osl_prefetch)(const void *ptr)
468 {
469 __asm__ __volatile__("pld\t%0" :: "o"(*(const char *)ptr) : "cc");
470 }
471
472 #endif /* !__ARM_ARCH_7A__ */
473
474 uint32
475 osl_pci_read_config(osl_t *osh, uint offset, uint size)
476 {
477 uint val = 0;
478 uint retry = PCI_CFG_RETRY; /* PR15065: faulty cardbus controller bug */
479
480 ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC)));
481
482 /* only 4byte access supported */
483 ASSERT(size == 4);
484
485 do {
486 pci_read_config_dword(osh->pdev, offset, &val);
487 if (val != 0xffffffff)
488 break;
489 } while (retry--);
490
491 #ifdef BCMDBG
492 if (retry < PCI_CFG_RETRY)
493 printk("PCI CONFIG READ access to %d required %d retries\n", offset,
494 (PCI_CFG_RETRY - retry));
495 #endif /* BCMDBG */
496
497 return (val);
498 }
499
500 void
501 osl_pci_write_config(osl_t *osh, uint offset, uint size, uint val)
502 {
503 uint retry = PCI_CFG_RETRY; /* PR15065: faulty cardbus controller bug */
504
505 ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC)));
506
507 /* only 4byte access supported */
508 ASSERT(size == 4);
509
510 do {
511 pci_write_config_dword(osh->pdev, offset, val);
512 /* PR15065: PCI_BAR0_WIN is believed to be the only pci cfg write that can occur
513 * when dma activity is possible
514 */
515 if (offset != PCI_BAR0_WIN)
516 break;
517 if (osl_pci_read_config(osh, offset, size) == val)
518 break;
519 } while (retry--);
520
521 #ifdef BCMDBG
522 if (retry < PCI_CFG_RETRY)
523 printk("PCI CONFIG WRITE access to %d required %d retries\n", offset,
524 (PCI_CFG_RETRY - retry));
525 #endif /* BCMDBG */
526 }
527
528 /* return bus # for the pci device pointed by osh->pdev */
529 uint
530 osl_pci_bus(osl_t *osh)
531 {
532 ASSERT(osh && (osh->magic == OS_HANDLE_MAGIC) && osh->pdev);
533
534 #if defined(__ARM_ARCH_7A__)
535 return pci_domain_nr(((struct pci_dev *)osh->pdev)->bus);
536 #else
537 return ((struct pci_dev *)osh->pdev)->bus->number;
538 #endif
539 }
540
541 /* return slot # for the pci device pointed by osh->pdev */
542 uint
543 osl_pci_slot(osl_t *osh)
544 {
545 ASSERT(osh && (osh->magic == OS_HANDLE_MAGIC) && osh->pdev);
546
547 #if defined(__ARM_ARCH_7A__)
548 return PCI_SLOT(((struct pci_dev *)osh->pdev)->devfn) + 1;
549 #else
550 return PCI_SLOT(((struct pci_dev *)osh->pdev)->devfn);
551 #endif
552 }
553
554 /* return domain # for the pci device pointed by osh->pdev */
555 uint
556 osl_pcie_domain(osl_t *osh)
557 {
558 ASSERT(osh && (osh->magic == OS_HANDLE_MAGIC) && osh->pdev);
559
560 return pci_domain_nr(((struct pci_dev *)osh->pdev)->bus);
561 }
562
563 /* return bus # for the pci device pointed by osh->pdev */
564 uint
565 osl_pcie_bus(osl_t *osh)
566 {
567 ASSERT(osh && (osh->magic == OS_HANDLE_MAGIC) && osh->pdev);
568
569 return ((struct pci_dev *)osh->pdev)->bus->number;
570 }
571
572 /* return the pci device pointed by osh->pdev */
573 struct pci_dev *
574 osl_pci_device(osl_t *osh)
575 {
576 ASSERT(osh && (osh->magic == OS_HANDLE_MAGIC) && osh->pdev);
577
578 return osh->pdev;
579 }
580
581 #ifdef BCMDBG_MEM
582 /* In BCMDBG_MEM configurations osl_malloc is only used internally in
583 * the implementation of osl_debug_malloc. Because we are using the GCC
584 * -Wstrict-prototypes compile option, we must always have a prototype
585 * for a global/external function. So make osl_malloc static in
586 * the BCMDBG_MEM case.
587 */
588 static
589 #endif
590 void *
591 osl_malloc(osl_t *osh, uint size)
592 {
593 void *addr;
594 gfp_t flags;
595
596 /* only ASSERT if osh is defined */
597 if (osh)
598 ASSERT(osh->magic == OS_HANDLE_MAGIC);
599 #ifdef CONFIG_DHD_USE_STATIC_BUF
600 if (bcm_static_buf)
601 {
602 unsigned long irq_flags;
603 int i = 0;
604 if ((size >= PAGE_SIZE)&&(size <= STATIC_BUF_SIZE))
605 {
606 OSL_STATIC_BUF_LOCK(&bcm_static_buf->static_lock, irq_flags);
607
608 for (i = 0; i < STATIC_BUF_MAX_NUM; i++)
609 {
610 if (bcm_static_buf->buf_use[i] == 0)
611 break;
612 }
613
614 if (i == STATIC_BUF_MAX_NUM)
615 {
616 OSL_STATIC_BUF_UNLOCK(&bcm_static_buf->static_lock, irq_flags);
617 printk("all static buff in use!\n");
618 goto original;
619 }
620
621 bcm_static_buf->buf_use[i] = 1;
622 OSL_STATIC_BUF_UNLOCK(&bcm_static_buf->static_lock, irq_flags);
623
624 bzero(bcm_static_buf->buf_ptr+STATIC_BUF_SIZE*i, size);
625 if (osh)
626 atomic_add(size, &osh->cmn->malloced);
627
628 return ((void *)(bcm_static_buf->buf_ptr+STATIC_BUF_SIZE*i));
629 }
630 }
631 original:
632 #endif /* CONFIG_DHD_USE_STATIC_BUF */
633
634 flags = CAN_SLEEP() ? GFP_KERNEL: GFP_ATOMIC;
635 if ((addr = kmalloc(size, flags)) == NULL) {
636 if (osh)
637 osh->failed++;
638 return (NULL);
639 }
640 if (osh && osh->cmn)
641 atomic_add(size, &osh->cmn->malloced);
642
643 return (addr);
644 }
645
646 #ifndef BCMDBG_MEM
647 void *
648 osl_mallocz(osl_t *osh, uint size)
649 {
650 void *ptr;
651
652 ptr = osl_malloc(osh, size);
653
654 if (ptr != NULL) {
655 bzero(ptr, size);
656 }
657
658 return ptr;
659 }
660 #endif
661
662 #ifdef BCMDBG_MEM
663 /* In BCMDBG_MEM configurations osl_mfree is only used internally in
664 * the implementation of osl_debug_mfree. Because we are using the GCC
665 * -Wstrict-prototypes compile option, we must always have a prototype
666 * for a global/external function. So make osl_mfree static in
667 * the BCMDBG_MEM case.
668 */
669 static
670 #endif
671 void
672 osl_mfree(osl_t *osh, void *addr, uint size)
673 {
674 #ifdef CONFIG_DHD_USE_STATIC_BUF
675 unsigned long flags;
676
677 if (addr == NULL) {
678 return;
679 }
680
681 if (bcm_static_buf)
682 {
683 if ((addr > (void *)bcm_static_buf) && ((unsigned char *)addr
684 <= ((unsigned char *)bcm_static_buf + STATIC_BUF_TOTAL_LEN)))
685 {
686 int buf_idx = 0;
687
688 buf_idx = ((unsigned char *)addr - bcm_static_buf->buf_ptr)/STATIC_BUF_SIZE;
689
690 OSL_STATIC_BUF_LOCK(&bcm_static_buf->static_lock, flags);
691 bcm_static_buf->buf_use[buf_idx] = 0;
692 OSL_STATIC_BUF_UNLOCK(&bcm_static_buf->static_lock, flags);
693
694 if (osh && osh->cmn) {
695 ASSERT(osh->magic == OS_HANDLE_MAGIC);
696 atomic_sub(size, &osh->cmn->malloced);
697 }
698 return;
699 }
700 }
701 #endif /* CONFIG_DHD_USE_STATIC_BUF */
702 if (osh && osh->cmn) {
703 ASSERT(osh->magic == OS_HANDLE_MAGIC);
704
705 ASSERT(size <= osl_malloced(osh));
706
707 atomic_sub(size, &osh->cmn->malloced);
708 }
709 kfree(addr);
710 }
711
712 #ifdef BCMDBG_MEM
713 /* In BCMDBG_MEM configurations osl_vmalloc is only used internally in
714 * the implementation of osl_debug_vmalloc. Because we are using the GCC
715 * -Wstrict-prototypes compile option, we must always have a prototype
716 * for a global/external function. So make osl_vmalloc static in
717 * the BCMDBG_MEM case.
718 */
719 static
720 #endif
721 void *
722 osl_vmalloc(osl_t *osh, uint size)
723 {
724 void *addr;
725
726 /* only ASSERT if osh is defined */
727 if (osh)
728 ASSERT(osh->magic == OS_HANDLE_MAGIC);
729 if ((addr = vmalloc(size)) == NULL) {
730 if (osh)
731 osh->failed++;
732 return (NULL);
733 }
734 if (osh && osh->cmn)
735 atomic_add(size, &osh->cmn->malloced);
736
737 return (addr);
738 }
739
740 #ifndef BCMDBG_MEM
741 void *
742 osl_vmallocz(osl_t *osh, uint size)
743 {
744 void *ptr;
745
746 ptr = osl_vmalloc(osh, size);
747
748 if (ptr != NULL) {
749 bzero(ptr, size);
750 }
751
752 return ptr;
753 }
754 #endif
755
756 #ifdef BCMDBG_MEM
757 /* In BCMDBG_MEM configurations osl_vmfree is only used internally in
758 * the implementation of osl_debug_vmfree. Because we are using the GCC
759 * -Wstrict-prototypes compile option, we must always have a prototype
760 * for a global/external function. So make osl_vmfree static in
761 * the BCMDBG_MEM case.
762 */
763 static
764 #endif
765 void
766 osl_vmfree(osl_t *osh, void *addr, uint size)
767 {
768 if (osh && osh->cmn) {
769 ASSERT(osh->magic == OS_HANDLE_MAGIC);
770
771 ASSERT(size <= osl_malloced(osh));
772
773 atomic_sub(size, &osh->cmn->malloced);
774 }
775 vfree(addr);
776 }
777
778 uint
779 osl_check_memleak(osl_t *osh)
780 {
781 ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC)));
782 if (atomic_read(&osh->cmn->refcount) == 1)
783 return (atomic_read(&osh->cmn->malloced));
784 else
785 return 0;
786 }
787
788 uint
789 osl_malloced(osl_t *osh)
790 {
791 ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC)));
792 return (atomic_read(&osh->cmn->malloced));
793 }
794
795 uint
796 osl_malloc_failed(osl_t *osh)
797 {
798 ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC)));
799 return (osh->failed);
800 }
801
802 #ifdef BCMDBG_MEM
803 void *
804 osl_debug_malloc(osl_t *osh, uint size, int line, const char* file)
805 {
806 bcm_mem_link_t *p;
807 const char* basename;
808 unsigned long flags = 0;
809 if (!size) {
810 printk("%s: allocating zero sized mem at %s line %d\n", __FUNCTION__, file, line);
811 ASSERT(0);
812 }
813
814 if ((p = (bcm_mem_link_t*)osl_malloc(osh, sizeof(bcm_mem_link_t) + size)) == NULL) {
815 return (NULL);
816 }
817
818 if (osh) {
819 OSL_MEMLIST_LOCK(&osh->cmn->dbgmem_lock, flags);
820 }
821
822 p->size = size;
823 p->line = line;
824 p->osh = (void *)osh;
825
826 basename = strrchr(file, '/');
827 /* skip the '/' */
828 if (basename)
829 basename++;
830
831 if (!basename)
832 basename = file;
833
834 strlcpy(p->file, basename, sizeof(p->file));
835
836 /* link this block */
837 if (osh) {
838 p->prev = NULL;
839 p->next = osh->cmn->dbgmem_list;
840 if (p->next)
841 p->next->prev = p;
842 osh->cmn->dbgmem_list = p;
843 OSL_MEMLIST_UNLOCK(&osh->cmn->dbgmem_lock, flags);
844 }
845
846 return p + 1;
847 }
848
849 void *
850 osl_debug_mallocz(osl_t *osh, uint size, int line, const char* file)
851 {
852 void *ptr;
853
854 ptr = osl_debug_malloc(osh, size, line, file);
855
856 if (ptr != NULL) {
857 bzero(ptr, size);
858 }
859
860 return ptr;
861 }
862
863 void
864 osl_debug_mfree(osl_t *osh, void *addr, uint size, int line, const char* file)
865 {
866 bcm_mem_link_t *p;
867 unsigned long flags = 0;
868
869 ASSERT(osh == NULL || osh->magic == OS_HANDLE_MAGIC);
870
871 if (addr == NULL) {
872 return;
873 }
874
875 p = (bcm_mem_link_t *)((int8*)addr - sizeof(bcm_mem_link_t));
876 if (p->size == 0) {
877 printk("osl_debug_mfree: double free on addr %p size %d at line %d file %s\n",
878 addr, size, line, file);
879 prhex("bcm_mem_link_t", (void *)p, sizeof(*p));
880 ASSERT(p->size);
881 return;
882 }
883
884 if (p->size != size) {
885 printk("%s: dealloca size does not match alloc size\n", __FUNCTION__);
886 printk("Dealloc addr %p size %d at line %d file %s\n", addr, size, line, file);
887 printk("Alloc size %d line %d file %s\n", p->size, p->line, p->file);
888 prhex("bcm_mem_link_t", (void *)p, sizeof(*p));
889 ASSERT(p->size == size);
890 return;
891 }
892
893 if (osh && ((osl_t*)p->osh)->cmn != osh->cmn) {
894 printk("osl_debug_mfree: alloc osh %p does not match dealloc osh %p\n",
895 ((osl_t*)p->osh)->cmn, osh->cmn);
896 printk("Dealloc addr %p size %d at line %d file %s\n", addr, size, line, file);
897 printk("Alloc size %d line %d file %s\n", p->size, p->line, p->file);
898 prhex("bcm_mem_link_t", (void *)p, sizeof(*p));
899 ASSERT(((osl_t*)p->osh)->cmn == osh->cmn);
900 return;
901 }
902
903 /* unlink this block */
904 if (osh && osh->cmn) {
905 OSL_MEMLIST_LOCK(&osh->cmn->dbgmem_lock, flags);
906 if (p->prev)
907 p->prev->next = p->next;
908 if (p->next)
909 p->next->prev = p->prev;
910 if (osh->cmn->dbgmem_list == p)
911 osh->cmn->dbgmem_list = p->next;
912 p->next = p->prev = NULL;
913 }
914 p->size = 0;
915
916 if (osh && osh->cmn) {
917 OSL_MEMLIST_UNLOCK(&osh->cmn->dbgmem_lock, flags);
918 }
919 osl_mfree(osh, p, size + sizeof(bcm_mem_link_t));
920 }
921
922 void *
923 osl_debug_vmalloc(osl_t *osh, uint size, int line, const char* file)
924 {
925 bcm_mem_link_t *p;
926 const char* basename;
927 unsigned long flags = 0;
928 if (!size) {
929 printk("%s: allocating zero sized mem at %s line %d\n", __FUNCTION__, file, line);
930 ASSERT(0);
931 }
932
933 if ((p = (bcm_mem_link_t*)osl_vmalloc(osh, sizeof(bcm_mem_link_t) + size)) == NULL) {
934 return (NULL);
935 }
936
937 if (osh) {
938 OSL_MEMLIST_LOCK(&osh->cmn->dbgmem_lock, flags);
939 }
940
941 p->size = size;
942 p->line = line;
943 p->osh = (void *)osh;
944
945 basename = strrchr(file, '/');
946 /* skip the '/' */
947 if (basename)
948 basename++;
949
950 if (!basename)
951 basename = file;
952
953 strlcpy(p->file, basename, sizeof(p->file));
954
955 /* link this block */
956 if (osh) {
957 p->prev = NULL;
958 p->next = osh->cmn->dbgvmem_list;
959 if (p->next)
960 p->next->prev = p;
961 osh->cmn->dbgvmem_list = p;
962 OSL_MEMLIST_UNLOCK(&osh->cmn->dbgmem_lock, flags);
963 }
964
965 return p + 1;
966 }
967
968 void *
969 osl_debug_vmallocz(osl_t *osh, uint size, int line, const char* file)
970 {
971 void *ptr;
972
973 ptr = osl_debug_vmalloc(osh, size, line, file);
974
975 if (ptr != NULL) {
976 bzero(ptr, size);
977 }
978
979 return ptr;
980 }
981
982 void
983 osl_debug_vmfree(osl_t *osh, void *addr, uint size, int line, const char* file)
984 {
985 bcm_mem_link_t *p = (bcm_mem_link_t *)((int8*)addr - sizeof(bcm_mem_link_t));
986 unsigned long flags = 0;
987
988 ASSERT(osh == NULL || osh->magic == OS_HANDLE_MAGIC);
989
990 if (p->size == 0) {
991 printk("osl_debug_mfree: double free on addr %p size %d at line %d file %s\n",
992 addr, size, line, file);
993 ASSERT(p->size);
994 return;
995 }
996
997 if (p->size != size) {
998 printk("%s: dealloca size does not match alloc size\n", __FUNCTION__);
999 printk("Dealloc addr %p size %d at line %d file %s\n", addr, size, line, file);
1000 printk("Alloc size %d line %d file %s\n", p->size, p->line, p->file);
1001 ASSERT(p->size == size);
1002 return;
1003 }
1004
1005 if (osh && ((osl_t*)p->osh)->cmn != osh->cmn) {
1006 printk("osl_debug_mfree: alloc osh %p does not match dealloc osh %p\n",
1007 ((osl_t*)p->osh)->cmn, osh->cmn);
1008 printk("Dealloc addr %p size %d at line %d file %s\n", addr, size, line, file);
1009 printk("Alloc size %d line %d file %s\n", p->size, p->line, p->file);
1010 ASSERT(((osl_t*)p->osh)->cmn == osh->cmn);
1011 return;
1012 }
1013
1014 /* unlink this block */
1015 if (osh && osh->cmn) {
1016 OSL_MEMLIST_LOCK(&osh->cmn->dbgmem_lock, flags);
1017 if (p->prev)
1018 p->prev->next = p->next;
1019 if (p->next)
1020 p->next->prev = p->prev;
1021 if (osh->cmn->dbgvmem_list == p)
1022 osh->cmn->dbgvmem_list = p->next;
1023 p->next = p->prev = NULL;
1024 }
1025 p->size = 0;
1026
1027 if (osh && osh->cmn) {
1028 OSL_MEMLIST_UNLOCK(&osh->cmn->dbgmem_lock, flags);
1029 }
1030 osl_vmfree(osh, p, size + sizeof(bcm_mem_link_t));
1031 }
1032
1033 int
1034 osl_debug_memdump(osl_t *osh, struct bcmstrbuf *b)
1035 {
1036 bcm_mem_link_t *p;
1037 unsigned long flags = 0;
1038
1039 ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC)));
1040
1041 OSL_MEMLIST_LOCK(&osh->cmn->dbgmem_lock, flags);
1042
1043 if (osl_check_memleak(osh) && osh->cmn->dbgmem_list) {
1044 if (b != NULL)
1045 bcm_bprintf(b, " Address Size File:line\n");
1046 else
1047 printk(" Address Size File:line\n");
1048
1049 for (p = osh->cmn->dbgmem_list; p; p = p->next) {
1050 if (b != NULL)
1051 bcm_bprintf(b, "%p %6d %s:%d\n", (char*)p + sizeof(bcm_mem_link_t),
1052 p->size, p->file, p->line);
1053 else
1054 printk("%p %6d %s:%d\n", (char*)p + sizeof(bcm_mem_link_t),
1055 p->size, p->file, p->line);
1056
1057 /* Detects loop-to-self so we don't enter infinite loop */
1058 if (p == p->next) {
1059 if (b != NULL)
1060 bcm_bprintf(b, "WARNING: loop-to-self "
1061 "p %p p->next %p\n", p, p->next);
1062 else
1063 printk("WARNING: loop-to-self "
1064 "p %p p->next %p\n", p, p->next);
1065
1066 break;
1067 }
1068 }
1069 }
1070 if (osl_check_memleak(osh) && osh->cmn->dbgvmem_list) {
1071 if (b != NULL)
1072 bcm_bprintf(b, "Vmem\n Address Size File:line\n");
1073 else
1074 printk("Vmem\n Address Size File:line\n");
1075
1076 for (p = osh->cmn->dbgvmem_list; p; p = p->next) {
1077 if (b != NULL)
1078 bcm_bprintf(b, "%p %6d %s:%d\n", (char*)p + sizeof(bcm_mem_link_t),
1079 p->size, p->file, p->line);
1080 else
1081 printk("%p %6d %s:%d\n", (char*)p + sizeof(bcm_mem_link_t),
1082 p->size, p->file, p->line);
1083
1084 /* Detects loop-to-self so we don't enter infinite loop */
1085 if (p == p->next) {
1086 if (b != NULL)
1087 bcm_bprintf(b, "WARNING: loop-to-self "
1088 "p %p p->next %p\n", p, p->next);
1089 else
1090 printk("WARNING: loop-to-self "
1091 "p %p p->next %p\n", p, p->next);
1092
1093 break;
1094 }
1095 }
1096 }
1097
1098 OSL_MEMLIST_UNLOCK(&osh->cmn->dbgmem_lock, flags);
1099
1100 return 0;
1101 }
1102
1103 #endif /* BCMDBG_MEM */
1104
1105 uint
1106 osl_dma_consistent_align(void)
1107 {
1108 return (PAGE_SIZE);
1109 }
1110
1111 void*
1112 osl_dma_alloc_consistent(osl_t *osh, uint size, uint16 align_bits, uint *alloced, dmaaddr_t *pap)
1113 {
1114 void *va;
1115 uint16 align = (1 << align_bits);
1116 ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC)));
1117
1118 if (!ISALIGNED(DMA_CONSISTENT_ALIGN, align))
1119 size += align;
1120 *alloced = size;
1121
1122 #if (defined(__ARM_ARCH_7A__) && !defined(DHD_USE_COHERENT_MEM_FOR_RING))
1123 va = kmalloc(size, GFP_ATOMIC | __GFP_ZERO);
1124 if (va)
1125 *pap = (ulong)__virt_to_phys((ulong)va);
1126 #else
1127 {
1128 dma_addr_t pap_lin;
1129 struct pci_dev *hwdev = osh->pdev;
1130 gfp_t flags;
1131 #ifdef DHD_ALLOC_COHERENT_MEM_FROM_ATOMIC_POOL
1132 flags = GFP_ATOMIC;
1133 #else
1134 flags = CAN_SLEEP() ? GFP_KERNEL: GFP_ATOMIC;
1135 #endif /* DHD_ALLOC_COHERENT_MEM_FROM_ATOMIC_POOL */
1136 #ifdef DHD_ALLOC_COHERENT_MEM_WITH_GFP_COMP
1137 flags |= __GFP_COMP;
1138 #endif /* DHD_ALLOC_COHERENT_MEM_WITH_GFP_COMP */
1139 va = dma_alloc_coherent(&hwdev->dev, size, &pap_lin, flags);
1140 #ifdef BCMDMA64OSL
1141 PHYSADDRLOSET(*pap, pap_lin & 0xffffffff);
1142 PHYSADDRHISET(*pap, (pap_lin >> 32) & 0xffffffff);
1143 #else
1144 *pap = (dmaaddr_t)pap_lin;
1145 #endif /* BCMDMA64OSL */
1146 }
1147 #endif /* __ARM_ARCH_7A__ && !DHD_USE_COHERENT_MEM_FOR_RING */
1148
1149 return va;
1150 }
1151
1152 void
1153 osl_dma_free_consistent(osl_t *osh, void *va, uint size, dmaaddr_t pa)
1154 {
1155 #ifdef BCMDMA64OSL
1156 dma_addr_t paddr;
1157 #endif /* BCMDMA64OSL */
1158 ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC)));
1159
1160 #if (defined(__ARM_ARCH_7A__) && !defined(DHD_USE_COHERENT_MEM_FOR_RING))
1161 kfree(va);
1162 #else
1163 #ifdef BCMDMA64OSL
1164 PHYSADDRTOULONG(pa, paddr);
1165 pci_free_consistent(osh->pdev, size, va, paddr);
1166 #else
1167 pci_free_consistent(osh->pdev, size, va, (dma_addr_t)pa);
1168 #endif /* BCMDMA64OSL */
1169 #endif /* __ARM_ARCH_7A__ && !DHD_USE_COHERENT_MEM_FOR_RING */
1170 }
1171
1172 void *
1173 osl_virt_to_phys(void *va)
1174 {
1175 return (void *)(uintptr)virt_to_phys(va);
1176 }
1177
1178 #include <asm/cacheflush.h>
1179 void
1180 BCMFASTPATH(osl_dma_flush)(osl_t *osh, void *va, uint size, int direction, void *p,
1181 hnddma_seg_map_t *dmah)
1182 {
1183 return;
1184 }
1185
1186 dmaaddr_t
1187 BCMFASTPATH(osl_dma_map)(osl_t *osh, void *va, uint size, int direction, void *p,
1188 hnddma_seg_map_t *dmah)
1189 {
1190 int dir;
1191 dmaaddr_t ret_addr;
1192 dma_addr_t map_addr;
1193 int ret;
1194
1195 DMA_LOCK(osh);
1196
1197 ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC)));
1198 dir = (direction == DMA_TX)? PCI_DMA_TODEVICE: PCI_DMA_FROMDEVICE;
1199
1200 map_addr = pci_map_single(osh->pdev, va, size, dir);
1201
1202 ret = pci_dma_mapping_error(osh->pdev, map_addr);
1203
1204 if (ret) {
1205 printk("%s: Failed to map memory\n", __FUNCTION__);
1206 PHYSADDRLOSET(ret_addr, 0);
1207 PHYSADDRHISET(ret_addr, 0);
1208 } else {
1209 PHYSADDRLOSET(ret_addr, map_addr & 0xffffffff);
1210 PHYSADDRHISET(ret_addr, (map_addr >> 32) & 0xffffffff);
1211 }
1212
1213 #ifdef DHD_MAP_LOGGING
1214 osl_dma_map_logging(osh, osh->dhd_map_log, ret_addr, size);
1215 #endif /* DHD_MAP_LOGGING */
1216
1217 DMA_UNLOCK(osh);
1218
1219 return ret_addr;
1220 }
1221
1222 void
1223 BCMFASTPATH(osl_dma_unmap)(osl_t *osh, dmaaddr_t pa, uint size, int direction)
1224 {
1225 int dir;
1226 #ifdef BCMDMA64OSL
1227 dma_addr_t paddr;
1228 #endif /* BCMDMA64OSL */
1229
1230 ASSERT((osh && (osh->magic == OS_HANDLE_MAGIC)));
1231
1232 DMA_LOCK(osh);
1233
1234 dir = (direction == DMA_TX)? PCI_DMA_TODEVICE: PCI_DMA_FROMDEVICE;
1235
1236 #ifdef DHD_MAP_LOGGING
1237 osl_dma_map_logging(osh, osh->dhd_unmap_log, pa, size);
1238 #endif /* DHD_MAP_LOGGING */
1239
1240 #ifdef BCMDMA64OSL
1241 PHYSADDRTOULONG(pa, paddr);
1242 pci_unmap_single(osh->pdev, paddr, size, dir);
1243 #else /* BCMDMA64OSL */
1244 pci_unmap_single(osh->pdev, (uint32)pa, size, dir);
1245 #endif /* BCMDMA64OSL */
1246
1247 DMA_UNLOCK(osh);
1248 }
1249
1250 /* OSL function for CPU relax */
1251 inline void
1252 BCMFASTPATH(osl_cpu_relax)(void)
1253 {
1254 cpu_relax();
1255 }
1256
1257 extern void osl_preempt_disable(osl_t *osh)
1258 {
1259 preempt_disable();
1260 }
1261
1262 extern void osl_preempt_enable(osl_t *osh)
1263 {
1264 preempt_enable();
1265 }
1266
1267 #if defined(BCMDBG_ASSERT) || defined(BCMASSERT_LOG)
1268 void
1269 osl_assert(const char *exp, const char *file, int line)
1270 {
1271 char tempbuf[256];
1272 const char *basename;
1273
1274 basename = strrchr(file, '/');
1275 /* skip the '/' */
1276 if (basename)
1277 basename++;
1278
1279 if (!basename)
1280 basename = file;
1281
1282 #ifdef BCMASSERT_LOG
1283 snprintf(tempbuf, 64, "\"%s\": file \"%s\", line %d\n",
1284 exp, basename, line);
1285 #ifndef OEM_ANDROID
1286 bcm_assert_log(tempbuf);
1287 #endif /* OEM_ANDROID */
1288 #endif /* BCMASSERT_LOG */
1289
1290 #ifdef BCMDBG_ASSERT
1291 snprintf(tempbuf, 256, "assertion \"%s\" failed: file \"%s\", line %d\n",
1292 exp, basename, line);
1293
1294 /* Print assert message and give it time to be written to /var/log/messages */
1295 if (!in_interrupt() && g_assert_type != 1 && g_assert_type != 3) {
1296 const int delay = 3;
1297 printk("%s", tempbuf);
1298 printk("panic in %d seconds\n", delay);
1299 set_current_state(TASK_INTERRUPTIBLE);
1300 schedule_timeout(delay * HZ);
1301 }
1302 #endif /* BCMDBG_ASSERT */
1303
1304 switch (g_assert_type) {
1305 case 0:
1306 printk("%s", tempbuf);
1307 BUG();
1308 break;
1309 case 1:
1310 /* fall through */
1311 case 3:
1312 printk("%s", tempbuf);
1313 break;
1314 case 2:
1315 printk("%s", tempbuf);
1316 BUG();
1317 break;
1318 default:
1319 break;
1320 }
1321 }
1322 #endif /* BCMDBG_ASSERT || BCMASSERT_LOG */
1323
1324 void
1325 osl_delay(uint usec)
1326 {
1327 uint d;
1328
1329 #ifdef BCMSLTGT
1330 usec *= htclkratio;
1331 #endif
1332
1333 while (usec > 0) {
1334 d = MIN(usec, 1000);
1335 udelay(d);
1336 usec -= d;
1337 }
1338 }
1339
1340 void
1341 osl_sleep(uint ms)
1342 {
1343 #ifdef BCMSLTGT
1344 ms *= htclkratio;
1345 #endif
1346
1347 if (ms < 20)
1348 usleep_range(ms*1000, ms*1000 + 1000);
1349 else
1350 msleep(ms);
1351 }
1352
1353 uint64
1354 osl_sysuptime_us(void)
1355 {
1356 struct osl_timespec tv;
1357 uint64 usec;
1358
1359 osl_do_gettimeofday(&tv);
1360 /* tv_usec content is fraction of a second */
1361 usec = (uint64)tv.tv_sec * 1000000ul + tv.tv_usec;
1362 #ifdef BCMSLTGT
1363 /* scale down the time to match the slow target roughly */
1364 usec /= htclkratio;
1365 #endif
1366 return usec;
1367 }
1368
1369 uint64
1370 osl_localtime_ns(void)
1371 {
1372 uint64 ts_nsec = 0;
1373
1374 #ifdef BCMDONGLEHOST
1375 /* Some Linux based platform cannot use local_clock()
1376 * since it is defined by EXPORT_SYMBOL_GPL().
1377 * GPL-incompatible module (NIC builds wl.ko)
1378 * cannnot use the GPL-only symbol.
1379 */
1380 ts_nsec = local_clock();
1381 #endif /* BCMDONGLEHOST */
1382 return ts_nsec;
1383 }
1384
1385 void
1386 osl_get_localtime(uint64 *sec, uint64 *usec)
1387 {
1388 uint64 ts_nsec = 0;
1389 unsigned long rem_nsec = 0;
1390
1391 #ifdef BCMDONGLEHOST
1392 /* Some Linux based platform cannot use local_clock()
1393 * since it is defined by EXPORT_SYMBOL_GPL().
1394 * GPL-incompatible module (NIC builds wl.ko) can
1395 * not use the GPL-only symbol.
1396 */
1397 ts_nsec = local_clock();
1398 rem_nsec = do_div(ts_nsec, NSEC_PER_SEC);
1399 #endif /* BCMDONGLEHOST */
1400 *sec = (uint64)ts_nsec;
1401 *usec = (uint64)(rem_nsec / MSEC_PER_SEC);
1402 }
1403
1404 uint64
1405 osl_systztime_us(void)
1406 {
1407 struct osl_timespec tv;
1408 uint64 tzusec;
1409
1410 osl_do_gettimeofday(&tv);
1411 /* apply timezone */
1412 tzusec = (uint64)((tv.tv_sec - (sys_tz.tz_minuteswest * 60)) *
1413 USEC_PER_SEC);
1414 tzusec += tv.tv_usec;
1415
1416 return tzusec;
1417 }
1418
1419 /*
1420 * OSLREGOPS specifies the use of osl_XXX routines to be used for register access
1421 */
1422 #ifdef OSLREGOPS
1423 uint8
1424 osl_readb(osl_t *osh, volatile uint8 *r)
1425 {
1426 osl_rreg_fn_t rreg = ((osl_pubinfo_t*)osh)->rreg_fn;
1427 void *ctx = ((osl_pubinfo_t*)osh)->reg_ctx;
1428
1429 return (uint8)((rreg)(ctx, (volatile void*)r, sizeof(uint8)));
1430 }
1431
1432 uint16
1433 osl_readw(osl_t *osh, volatile uint16 *r)
1434 {
1435 osl_rreg_fn_t rreg = ((osl_pubinfo_t*)osh)->rreg_fn;
1436 void *ctx = ((osl_pubinfo_t*)osh)->reg_ctx;
1437
1438 return (uint16)((rreg)(ctx, (volatile void*)r, sizeof(uint16)));
1439 }
1440
1441 uint32
1442 osl_readl(osl_t *osh, volatile uint32 *r)
1443 {
1444 osl_rreg_fn_t rreg = ((osl_pubinfo_t*)osh)->rreg_fn;
1445 void *ctx = ((osl_pubinfo_t*)osh)->reg_ctx;
1446
1447 return (uint32)((rreg)(ctx, (volatile void*)r, sizeof(uint32)));
1448 }
1449
1450 void
1451 osl_writeb(osl_t *osh, volatile uint8 *r, uint8 v)
1452 {
1453 osl_wreg_fn_t wreg = ((osl_pubinfo_t*)osh)->wreg_fn;
1454 void *ctx = ((osl_pubinfo_t*)osh)->reg_ctx;
1455
1456 ((wreg)(ctx, (volatile void*)r, v, sizeof(uint8)));
1457 }
1458
1459 void
1460 osl_writew(osl_t *osh, volatile uint16 *r, uint16 v)
1461 {
1462 osl_wreg_fn_t wreg = ((osl_pubinfo_t*)osh)->wreg_fn;
1463 void *ctx = ((osl_pubinfo_t*)osh)->reg_ctx;
1464
1465 ((wreg)(ctx, (volatile void*)r, v, sizeof(uint16)));
1466 }
1467
1468 void
1469 osl_writel(osl_t *osh, volatile uint32 *r, uint32 v)
1470 {
1471 osl_wreg_fn_t wreg = ((osl_pubinfo_t*)osh)->wreg_fn;
1472 void *ctx = ((osl_pubinfo_t*)osh)->reg_ctx;
1473
1474 ((wreg)(ctx, (volatile void*)r, v, sizeof(uint32)));
1475 }
1476 #endif /* OSLREGOPS */
1477
1478 /*
1479 * BINOSL selects the slightly slower function-call-based binary compatible osl.
1480 */
1481 #ifdef BINOSL
1482
1483 uint32
1484 osl_sysuptime(void)
1485 {
1486 uint32 msec = ((uint32)jiffies * (1000 / HZ));
1487 #ifdef BCMSLTGT
1488 /* scale down the time to match the slow target roughly */
1489 msec /= htclkratio;
1490 #endif
1491 return msec;
1492 }
1493
1494 int
1495 osl_printf(const char *format, ...)
1496 {
1497 va_list args;
1498 static char printbuf[1024];
1499 int len;
1500
1501 /* sprintf into a local buffer because there *is* no "vprintk()".. */
1502 va_start(args, format);
1503 len = vsnprintf(printbuf, 1024, format, args);
1504 va_end(args);
1505
1506 if (len > sizeof(printbuf)) {
1507 printk("osl_printf: buffer overrun\n");
1508 return (0);
1509 }
1510
1511 return (printk("%s", printbuf));
1512 }
1513
1514 int
1515 osl_sprintf(char *buf, const char *format, ...)
1516 {
1517 va_list args;
1518 int rc;
1519
1520 va_start(args, format);
1521 rc = vsprintf(buf, format, args);
1522 va_end(args);
1523 return (rc);
1524 }
1525
1526 int
1527 osl_snprintf(char *buf, size_t n, const char *format, ...)
1528 {
1529 va_list args;
1530 int rc;
1531
1532 va_start(args, format);
1533 rc = vsnprintf(buf, n, format, args);
1534 va_end(args);
1535 return (rc);
1536 }
1537
1538 int
1539 osl_vsprintf(char *buf, const char *format, va_list ap)
1540 {
1541 return (vsprintf(buf, format, ap));
1542 }
1543
1544 int
1545 osl_vsnprintf(char *buf, size_t n, const char *format, va_list ap)
1546 {
1547 return (vsnprintf(buf, n, format, ap));
1548 }
1549
1550 int
1551 osl_strcmp(const char *s1, const char *s2)
1552 {
1553 return (strcmp(s1, s2));
1554 }
1555
1556 int
1557 osl_strncmp(const char *s1, const char *s2, uint n)
1558 {
1559 return (strncmp(s1, s2, n));
1560 }
1561
1562 int
1563 osl_strlen(const char *s)
1564 {
1565 return (strlen(s));
1566 }
1567
1568 char*
1569 osl_strcpy(char *d, const char *s)
1570 {
1571 return (strcpy(d, s));
1572 }
1573
1574 char*
1575 osl_strncpy(char *d, const char *s, uint n)
1576 {
1577 return (strlcpy(d, s, n));
1578 }
1579
1580 char*
1581 osl_strchr(const char *s, int c)
1582 {
1583 return (strchr(s, c));
1584 }
1585
1586 char*
1587 osl_strrchr(const char *s, int c)
1588 {
1589 return (strrchr(s, c));
1590 }
1591
1592 void*
1593 osl_memset(void *d, int c, size_t n)
1594 {
1595 return memset(d, c, n);
1596 }
1597
1598 void*
1599 osl_memcpy(void *d, const void *s, size_t n)
1600 {
1601 return memcpy(d, s, n);
1602 }
1603
1604 void*
1605 osl_memmove(void *d, const void *s, size_t n)
1606 {
1607 return memmove(d, s, n);
1608 }
1609
1610 int
1611 osl_memcmp(const void *s1, const void *s2, size_t n)
1612 {
1613 return memcmp(s1, s2, n);
1614 }
1615
1616 uint32
1617 osl_readl(volatile uint32 *r)
1618 {
1619 return (readl(r));
1620 }
1621
1622 uint16
1623 osl_readw(volatile uint16 *r)
1624 {
1625 return (readw(r));
1626 }
1627
1628 uint8
1629 osl_readb(volatile uint8 *r)
1630 {
1631 return (readb(r));
1632 }
1633
1634 void
1635 osl_writel(uint32 v, volatile uint32 *r)
1636 {
1637 writel(v, r);
1638 }
1639
1640 void
1641 osl_writew(uint16 v, volatile uint16 *r)
1642 {
1643 writew(v, r);
1644 }
1645
1646 void
1647 osl_writeb(uint8 v, volatile uint8 *r)
1648 {
1649 writeb(v, r);
1650 }
1651
1652 void *
1653 osl_uncached(void *va)
1654 {
1655 return ((void*)va);
1656 }
1657
1658 void *
1659 osl_cached(void *va)
1660 {
1661 return ((void*)va);
1662 }
1663
1664 uint
1665 osl_getcycles(void)
1666 {
1667 uint cycles;
1668
1669 #if defined(__i386__)
1670 rdtscl(cycles);
1671 #else
1672 cycles = 0;
1673 #endif /* __i386__ */
1674 return cycles;
1675 }
1676
1677 void *
1678 osl_reg_map(uint32 pa, uint size)
1679 {
1680 return (ioremap_nocache((unsigned long)pa, (unsigned long)size));
1681 }
1682
1683 void
1684 osl_reg_unmap(void *va)
1685 {
1686 iounmap(va);
1687 }
1688
1689 int
1690 osl_busprobe(uint32 *val, uint32 addr)
1691 {
1692 *val = readl((uint32 *)(uintptr)addr);
1693
1694 return 0;
1695 }
1696 #endif /* BINOSL */
1697
1698 uint32
1699 osl_rand(void)
1700 {
1701 uint32 rand;
1702
1703 get_random_bytes(&rand, sizeof(rand));
1704
1705 return rand;
1706 }
1707
1708 /* Linux Kernel: File Operations: start */
1709 void *
1710 osl_os_open_image(char *filename)
1711 {
1712 struct file *fp;
1713
1714 fp = filp_open(filename, O_RDONLY, 0);
1715 /*
1716 * 2.6.11 (FC4) supports filp_open() but later revs don't?
1717 * Alternative:
1718 * fp = open_namei(AT_FDCWD, filename, O_RD, 0);
1719 * ???
1720 */
1721 if (IS_ERR(fp)) {
1722 printk("ERROR %ld: Unable to open file %s\n", PTR_ERR(fp), filename);
1723 fp = NULL;
1724 }
1725
1726 return fp;
1727 }
1728
1729 int
1730 osl_os_get_image_block(char *buf, int len, void *image)
1731 {
1732 struct file *fp = (struct file *)image;
1733 int rdlen;
1734
1735 if (fp == NULL) {
1736 return 0;
1737 }
1738
1739 rdlen = kernel_read_compat(fp, fp->f_pos, buf, len);
1740 if (rdlen > 0) {
1741 fp->f_pos += rdlen;
1742 }
1743
1744 return rdlen;
1745 }
1746
1747 void
1748 osl_os_close_image(void *image)
1749 {
1750 struct file *fp = (struct file *)image;
1751
1752 if (fp != NULL) {
1753 filp_close(fp, NULL);
1754 }
1755 }
1756
1757 int
1758 osl_os_image_size(void *image)
1759 {
1760 int len = 0, curroffset;
1761
1762 if (image) {
1763 /* store the current offset */
1764 curroffset = generic_file_llseek(image, 0, 1);
1765 /* goto end of file to get length */
1766 len = generic_file_llseek(image, 0, 2);
1767 /* restore back the offset */
1768 generic_file_llseek(image, curroffset, 0);
1769 }
1770 return len;
1771 }
1772
1773 /* Linux Kernel: File Operations: end */
1774
1775 #if defined(AXI_TIMEOUTS_NIC)
1776 inline void osl_bpt_rreg(osl_t *osh, ulong addr, volatile void *v, uint size)
1777 {
1778 bool poll_timeout = FALSE;
1779 static int in_si_clear = FALSE;
1780
1781 switch (size) {
1782 case sizeof(uint8):
1783 *(volatile uint8*)v = readb((volatile uint8*)(addr));
1784 if (*(volatile uint8*)v == 0xff)
1785 poll_timeout = TRUE;
1786 break;
1787 case sizeof(uint16):
1788 *(volatile uint16*)v = readw((volatile uint16*)(addr));
1789 if (*(volatile uint16*)v == 0xffff)
1790 poll_timeout = TRUE;
1791 break;
1792 case sizeof(uint32):
1793 *(volatile uint32*)v = readl((volatile uint32*)(addr));
1794 if (*(volatile uint32*)v == 0xffffffff)
1795 poll_timeout = TRUE;
1796 break;
1797 case sizeof(uint64):
1798 *(volatile uint64*)v = *((volatile uint64*)(addr));
1799 if (*(volatile uint64*)v == 0xffffffffffffffff)
1800 poll_timeout = TRUE;
1801 break;
1802 }
1803
1804 if (osh && osh->sih && (in_si_clear == FALSE) && poll_timeout && osh->bpt_cb) {
1805 in_si_clear = TRUE;
1806 osh->bpt_cb((void *)osh->sih, (void *)addr);
1807 in_si_clear = FALSE;
1808 }
1809 }
1810 #endif /* AXI_TIMEOUTS_NIC */
1811
1812 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0)
1813 void
1814 timer_cb_compat(struct timer_list *tl)
1815 {
1816 timer_list_compat_t *t = container_of(tl, timer_list_compat_t, timer);
1817 t->callback((ulong)t->arg);
1818 }
1819 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0) */
1820
1821 /* timer apis */
1822 /* Note: All timer api's are thread unsafe and should be protected with locks by caller */
1823
1824 osl_timer_t *
1825 osl_timer_init(osl_t *osh, const char *name, void (*fn)(void *arg), void *arg)
1826 {
1827 osl_timer_t *t;
1828 BCM_REFERENCE(fn);
1829 if ((t = MALLOCZ(NULL, sizeof(osl_timer_t))) == NULL) {
1830 printk(KERN_ERR "osl_timer_init: out of memory, malloced %d bytes\n",
1831 (int)sizeof(osl_timer_t));
1832 return (NULL);
1833 }
1834 bzero(t, sizeof(osl_timer_t));
1835 if ((t->timer = MALLOCZ(NULL, sizeof(timer_list_compat_t))) == NULL) {
1836 printf("osl_timer_init: malloc failed\n");
1837 MFREE(NULL, t, sizeof(osl_timer_t));
1838 return (NULL);
1839 }
1840
1841 t->set = TRUE;
1842 #ifdef BCMDBG
1843 if ((t->name = MALLOCZ(NULL, strlen(name) + 1)) != NULL) {
1844 strcpy(t->name, name);
1845 }
1846 #endif
1847
1848 init_timer_compat(t->timer, (linux_timer_fn)fn, arg);
1849
1850 return (t);
1851 }
1852
1853 void
1854 osl_timer_add(osl_t *osh, osl_timer_t *t, uint32 ms, bool periodic)
1855 {
1856 if (t == NULL) {
1857 printf("%s: Timer handle is NULL\n", __FUNCTION__);
1858 return;
1859 }
1860 ASSERT(!t->set);
1861
1862 t->set = TRUE;
1863 if (periodic) {
1864 printf("Periodic timers are not supported by Linux timer apis\n");
1865 }
1866 #if defined(BCMSLTGT)
1867 timer_expires(t->timer) = jiffies + ms*HZ/1000*htclkratio;
1868 #else
1869 timer_expires(t->timer) = jiffies + ms*HZ/1000;
1870 #endif /* defined(BCMSLTGT) */
1871
1872 add_timer(t->timer);
1873
1874 return;
1875 }
1876
1877 void
1878 osl_timer_update(osl_t *osh, osl_timer_t *t, uint32 ms, bool periodic)
1879 {
1880 if (t == NULL) {
1881 printf("%s: Timer handle is NULL\n", __FUNCTION__);
1882 return;
1883 }
1884 if (periodic) {
1885 printf("Periodic timers are not supported by Linux timer apis\n");
1886 }
1887 t->set = TRUE;
1888 #if defined(BCMSLTGT)
1889 timer_expires(t->timer) = jiffies + ms*HZ/1000*htclkratio;
1890 #else
1891 timer_expires(t->timer) = jiffies + ms*HZ/1000;
1892 #endif /* defined(BCMSLTGT) */
1893
1894 mod_timer(t->timer, timer_expires(t->timer));
1895
1896 return;
1897 }
1898
1899 /*
1900 * Return TRUE if timer successfully deleted, FALSE if still pending
1901 */
1902 bool
1903 osl_timer_del(osl_t *osh, osl_timer_t *t)
1904 {
1905 if (t == NULL) {
1906 printf("%s: Timer handle is NULL\n", __FUNCTION__);
1907 return (FALSE);
1908 }
1909 if (t->set) {
1910 t->set = FALSE;
1911 if (t->timer) {
1912 del_timer(t->timer);
1913 MFREE(NULL, t->timer, sizeof(struct timer_list));
1914 }
1915 #ifdef BCMDBG
1916 if (t->name) {
1917 MFREE(NULL, t->name, strlen(t->name) + 1);
1918 }
1919 #endif
1920 MFREE(NULL, t, sizeof(osl_timer_t));
1921 }
1922 return (TRUE);
1923 }
1924
1925 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0))
1926 int
1927 kernel_read_compat(struct file *file, loff_t offset, char *addr, unsigned long count)
1928 {
1929 return (int)kernel_read(file, addr, (size_t)count, &offset);
1930 }
1931 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)) */
1932
1933 /* Linux specific multipurpose spinlock API */
1934 void *
1935 osl_spin_lock_init(osl_t *osh)
1936 {
1937 /* Adding 4 bytes since the sizeof(spinlock_t) could be 0 */
1938 /* if CONFIG_SMP and CONFIG_DEBUG_SPINLOCK are not defined */
1939 /* and this results in kernel asserts in internal builds */
1940 spinlock_t * lock = MALLOC(osh, sizeof(spinlock_t) + 4);
1941 if (lock)
1942 spin_lock_init(lock);
1943 return ((void *)lock);
1944 }
1945 void
1946 osl_spin_lock_deinit(osl_t *osh, void *lock)
1947 {
1948 if (lock)
1949 MFREE(osh, lock, sizeof(spinlock_t) + 4);
1950 }
1951
1952 unsigned long
1953 osl_spin_lock(void *lock)
1954 {
1955 unsigned long flags = 0;
1956
1957 if (lock) {
1958 #ifdef DHD_USE_SPIN_LOCK_BH
1959 /* Calling spin_lock_bh with both irq and non-irq context will lead to deadlock */
1960 ASSERT(!in_irq());
1961 spin_lock_bh((spinlock_t *)lock);
1962 #else
1963 spin_lock_irqsave((spinlock_t *)lock, flags);
1964 #endif /* DHD_USE_SPIN_LOCK_BH */
1965 }
1966
1967 return flags;
1968 }
1969
1970 void
1971 osl_spin_unlock(void *lock, unsigned long flags)
1972 {
1973 if (lock) {
1974 #ifdef DHD_USE_SPIN_LOCK_BH
1975 /* Calling spin_lock_bh with both irq and non-irq context will lead to deadlock */
1976 ASSERT(!in_irq());
1977 spin_unlock_bh((spinlock_t *)lock);
1978 #else
1979 spin_unlock_irqrestore((spinlock_t *)lock, flags);
1980 #endif /* DHD_USE_SPIN_LOCK_BH */
1981 }
1982 }
1983
1984 unsigned long
1985 osl_spin_lock_irq(void *lock)
1986 {
1987 unsigned long flags = 0;
1988
1989 if (lock)
1990 spin_lock_irqsave((spinlock_t *)lock, flags);
1991
1992 return flags;
1993 }
1994
1995 void
1996 osl_spin_unlock_irq(void *lock, unsigned long flags)
1997 {
1998 if (lock)
1999 spin_unlock_irqrestore((spinlock_t *)lock, flags);
2000 }
2001
2002 unsigned long
2003 osl_spin_lock_bh(void *lock)
2004 {
2005 unsigned long flags = 0;
2006
2007 if (lock) {
2008 /* Calling spin_lock_bh with both irq and non-irq context will lead to deadlock */
2009 ASSERT(!in_irq());
2010 spin_lock_bh((spinlock_t *)lock);
2011 }
2012
2013 return flags;
2014 }
2015
2016 void
2017 osl_spin_unlock_bh(void *lock, unsigned long flags)
2018 {
2019 if (lock) {
2020 /* Calling spin_lock_bh with both irq and non-irq context will lead to deadlock */
2021 ASSERT(!in_irq());
2022 spin_unlock_bh((spinlock_t *)lock);
2023 }
2024 }
2025
2026 void *
2027 osl_mutex_lock_init(osl_t *osh)
2028 {
2029 struct mutex *mtx = NULL;
2030
2031 mtx = MALLOCZ(osh, sizeof(*mtx));
2032 if (mtx)
2033 mutex_init(mtx);
2034
2035 return mtx;
2036 }
2037
2038 void
2039 osl_mutex_lock_deinit(osl_t *osh, void *mutex)
2040 {
2041 struct mutex *mtx = mutex;
2042
2043 if (mtx) {
2044 mutex_destroy(mtx);
2045 MFREE(osh, mtx, sizeof(struct mutex));
2046 }
2047 }
2048
2049 /* For mutex lock/unlock unsigned long flags is used,
2050 * this is to keep in sync with spin lock apis, so that
2051 * locks can be easily interchanged based on contexts
2052 */
2053 unsigned long
2054 osl_mutex_lock(void *lock)
2055 {
2056 if (lock)
2057 mutex_lock((struct mutex *)lock);
2058
2059 return 0;
2060 }
2061
2062 void
2063 osl_mutex_unlock(void *lock, unsigned long flags)
2064 {
2065 if (lock)
2066 mutex_unlock((struct mutex *)lock);
2067 return;
2068 }
2069
2070 #ifdef USE_DMA_LOCK
2071 static void
2072 osl_dma_lock(osl_t *osh)
2073 {
2074 /* The conditional check is to avoid the scheduling bug.
2075 * If the spin_lock_bh is used under the spin_lock_irqsave,
2076 * Kernel triggered the warning message as the spin_lock_irqsave
2077 * disables the interrupt and the spin_lock_bh doesn't use in case
2078 * interrupt is disabled.
2079 * Please refer to the __local_bh_enable_ip() function
2080 * in kernel/softirq.c to understand the condtion.
2081 */
2082 if (likely(in_irq() || irqs_disabled())) {
2083 spin_lock(&osh->dma_lock);
2084 } else {
2085 spin_lock_bh(&osh->dma_lock);
2086 osh->dma_lock_bh = TRUE;
2087 }
2088 }
2089
2090 static void
2091 osl_dma_unlock(osl_t *osh)
2092 {
2093 if (unlikely(osh->dma_lock_bh)) {
2094 osh->dma_lock_bh = FALSE;
2095 spin_unlock_bh(&osh->dma_lock);
2096 } else {
2097 spin_unlock(&osh->dma_lock);
2098 }
2099 }
2100
2101 static void
2102 osl_dma_lock_init(osl_t *osh)
2103 {
2104 spin_lock_init(&osh->dma_lock);
2105 osh->dma_lock_bh = FALSE;
2106 }
2107 #endif /* USE_DMA_LOCK */
2108
2109 void
2110 osl_do_gettimeofday(struct osl_timespec *ts)
2111 {
2112 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
2113 struct timespec64 curtime;
2114 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
2115 struct timespec curtime;
2116 #else
2117 struct timeval curtime;
2118 #endif
2119
2120 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
2121 ktime_get_real_ts64(&curtime);
2122 ts->tv_nsec = curtime.tv_nsec;
2123 ts->tv_usec = curtime.tv_nsec / 1000;
2124 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
2125 getnstimeofday(&curtime);
2126 ts->tv_nsec = curtime.tv_nsec;
2127 ts->tv_usec = curtime.tv_nsec / 1000;
2128 #else
2129 do_gettimeofday(&curtime);
2130 ts->tv_usec = curtime.tv_usec;
2131 ts->tv_nsec = curtime.tv_usec * 1000;
2132 #endif
2133 ts->tv_sec = curtime.tv_sec;
2134 }
2135
2136 uint32
2137 osl_do_gettimediff(struct osl_timespec *cur_ts, struct osl_timespec *old_ts)
2138 {
2139 uint32 diff_s, diff_us, total_diff_us;
2140 bool pgc_g = FALSE;
2141
2142 diff_s = (uint32)cur_ts->tv_sec - (uint32)old_ts->tv_sec;
2143 pgc_g = (cur_ts->tv_usec > old_ts->tv_usec) ? TRUE : FALSE;
2144 diff_us = pgc_g ? (cur_ts->tv_usec - old_ts->tv_usec) : (old_ts->tv_usec - cur_ts->tv_usec);
2145 total_diff_us = pgc_g ? (diff_s * 1000000 + diff_us) : (diff_s * 1000000 - diff_us);
2146 return total_diff_us;
2147 }
2148
2149 void
2150 osl_get_monotonic_boottime(struct osl_timespec *ts)
2151 {
2152 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
2153 struct timespec64 curtime;
2154 #else
2155 struct timespec curtime;
2156 #endif
2157
2158 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
2159 curtime = ktime_to_timespec64(ktime_get_boottime());
2160 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2161 curtime = ktime_to_timespec(ktime_get_boottime());
2162 #else
2163 get_monotonic_boottime(&curtime);
2164 #endif
2165 ts->tv_sec = curtime.tv_sec;
2166 ts->tv_nsec = curtime.tv_nsec;
2167 ts->tv_usec = curtime.tv_nsec / 1000;
2168 }