Commit graph

1086 commits

Author SHA1 Message Date
Doug Moore
b0056b31e9 libkern: add ilog2 macro
The kernel source contains several definitions of an ilog2 function;
some are slower than necessary, and one of them is incorrect.
Elimininate them all and define an ilog2 macro in libkern to replace
them, in a way that is fast, correct for all argument types, and, in a
GENERIC kernel, includes a check for an invalid zero parameter.

Folks at Microsoft have verified that having a correct ilog2
definition for their MANA driver doesn't break it.

Reviewed by:	alc, markj, mhorne (older version), jhibbits (older version)
Differential Revision:	https://reviews.freebsd.org/D45170
Differential Revision:	https://reviews.freebsd.org/D45235
2024-06-03 11:37:55 -05:00
Bjoern A. Zeeb
ecd1d1f1da LinuxKPI: remove extern from function declarations
Sponsored by:	The FreeBSD Foundation
MFC after:	3 days
Reviewed by:	emaste (earlier version, found another two)
Differential Revision: https://reviews.freebsd.org/D45386
2024-05-30 21:01:57 +00:00
Bjoern A. Zeeb
bb025df257 LinuxKPI: add FIELD_PREP_CONST()
Add FIELD_PREP_CONST() like FIELD_PREP() without any extra checks likely
expected on this version in Linux.  This is called by an updated wireless
driver.

Sposnored by:	The FreeBSD Foundation
MFC after:	3 days
Reviewed by:	emaste
Differential Revision: https://reviews.freebsd.org/D45180
2024-05-21 23:27:10 +00:00
Bjoern A. Zeeb
8e4b8e9d80 LinuxKPI: add kvmemdup()
Add kvmemdup() as a variant of kmemdup().  While currently it could
just call kmemdup() we duplicate the code and use kvmalloc() in case
someone will change the implementation of kvmalloc/kvfree in slab.h.
This is used by an updated wireless driver.

Sponsored by:	The FreeBSD Foundation
MFC after:	3 days
Reviewed by:	emaste
Differential Revision: https://reviews.freebsd.org/D45181
2024-05-21 22:14:50 +00:00
Emmanuel Vadot
a7b222db12 linuxkpi: rwlock: Fix rwlock_init
Some linux code re-init some spinlock so add MTX_NEW to mtx_init.

Reported by:    rlibby
Fixes:          5c0a192348 ("linuxkpi: rwlock: Simplify code")
2024-05-17 07:59:02 +02:00
Emmanuel Vadot
cff79fd026 linuxkpi: Fix spin_lock_init
Some linux code re-init some spinlock so add MTX_NEW to mtx_init.

Reported by:	David Wolfskill <david@catwhisker.org>
Fixes:		ae38a1a1bf ("linuxkpi: spinlock: Simplify code")
2024-05-17 07:58:59 +02:00
Austin Shafer
28a59100b5 linuxkpi: Provide a non-NULL value for THIS_MODULE
THIS_MODULE is used to differentiate modules on Linux. We currently
completely stub out any Linux struct module usage, but THIS_MODULE
is still used to populate the "owner" fields of various drivers.
Even though we don't actually dereference these "owner" fields they
are still used by drivers to check if devices/dmabufs/etc come
from different modules. For example, during DRM GEM import some
drivers check if the dmabuf's owner matches the dev's owner. If
they match because they are both NULL drivers may incorrectly think
two resources come from the same module.

This adds a general purpose __this_linker_file which will point to
the linker file of the module that uses it. We can then use that
pointer to have a valid value for THIS_MODULE.

Reviewed by:	bz, jhb
Differential Revision:	https://reviews.freebsd.org/D44306
2024-05-16 10:36:36 -07:00
Emmanuel Vadot
abb1a1340e linuxkpi: refcount: Use atomic_t directly
Simply use a typedef for refcount_t on atomic_t, this allow us
to use a nativ type and also changing struct kref to directly use
a refcount_t like Linux.

Reviewed by:		bz
Sponsored by:		Beckhoff Automation GmbH & Co. KG
Differential Revision:	https://reviews.freebsd.org/D45207
2024-05-16 09:40:46 +02:00
Emmanuel Vadot
5c0a192348 linuxkpi: rwlock: Simplify code
Just use a typedef for rwlock_t, no need to create a useless
structure.

Reviewed by:		bz
Sponsored by:		Beckhoff Automation GmbH & Co. KG
Differential Revision:	https://reviews.freebsd.org/D45206
2024-05-16 09:40:03 +02:00
Emmanuel Vadot
ae38a1a1bf linuxkpi: spinlock: Simplify code
Just use a typedef for spinlock_t, no need to create a useless
structure.

Reviewed by:		bz, emaste
Sponsored by:		Beckhoff Automation GmbH & Co. KG
Differential Revision:	https://reviews.freebsd.org/D45205
2024-05-16 09:39:36 +02:00
Elliott Mitchell
a9e0f316b3 kern/intr: redeclare intr_setaffinity()'s third arg constant
This matches reality and allows removal of a __DECONST().

Fixes: 4c72d075a5 ("LinuxKPI: const argument to irq_set_affinity_hint()")
Fixes: 9b33b154b5 ("Add support to cpuset for binding hardware interrupts")
Reviewed by: imp
Pull Request: https://github.com/freebsd/freebsd-src/pull/1126
2024-05-09 17:14:35 -06:00
Tijl Coosemans
2ae0f5a4d0 linuxkpi: Make arch_io_*_memtype_wc amd64-only
Linux only implements these functions on x86.  They return 0 on other
architectures.  The FreeBSD implementation calls PHYS_TO_DMAP but this
panics on i386 because it does not have a direct map so return 0 on i386
as well for now.  These functions are only used by graphics/drm-*-kmod
to mark the VRAM aperture write-combining but this is also accomplished
by a call to vm_phys_fictitious_reg_range so this change is sufficient
to fix drm-*-kmod on i386 for FreeBSD 14.1.

Reviewed by:	kib
MFC after:	3 days
Differential Revision:	https://reviews.freebsd.org/D45125
2024-05-08 20:52:37 +02:00
Emmanuel Vadot
11d79c4756 linuxkpi: Add linuxkpi_video module
This contain the hdmi code and the aperture code like in linux.

Differential Revision:	https://reviews.freebsd.org/D44925
Reviewed by:		bz
Obtained from:		drm-kmod
Sponsored by:		Beckhoff Automation GmbH & Co. KG
2024-04-30 07:42:31 +02:00
Bjoern A. Zeeb
563c72e213 LinuxKPI: utsname.h add missing SPDX-License-Identifier
Reported by:	markj (2023-05-24; sorry took me a while)
MFC after:	3 days
2024-04-19 18:23:12 +00:00
Bjoern A. Zeeb
2a8c50592f LinuxKPI: add __counted_by() and __nonstring
Add checks for __counted_by(_x) and __nonstring as and depending
on compiler support enable the attribute/feature.

This is needed to make mtk76 and ath1xk drivers compile.

Sposnored by:	The FreeBSD Foundation
MFC after:	3 days
Reviewed by:	emaste, dim
Differential Revision: https://reviews.freebsd.org/D44587
2024-04-15 19:31:49 +00:00
Bjoern A. Zeeb
170c2e0e2b LinuxKPI: add devm_kfree()
mt76 calls devm_kfree() directly, so alias it to our
lkpi_devm_kmalloc_release() function.

Sponsored by:	The FreeBSD Foundation
MFC after:	3 days
Differential Revision: https://reviews.freebsd.org/D44589
2024-04-15 16:55:59 +00:00
Bjoern A. Zeeb
21761f2ede LinuxKPI: napi_schedule() requires return value, implement napi_is_scheduled()
A newer version of iwlwifi requires a return value from napi_schedule();
unclear if the function always should have been bool. Add the bool to test
based on the napi_schedule_prep() result.

Also add napi_is_scheduled() for rtw89.

Sponsored by:	The FreeBSD Foundation
MFC after:	3 days
Reviewed by:	emaste (previous version)
Differential Revision:	https://reviews.freebsd.org/D44591
2024-04-15 16:19:47 +00:00
Bjoern A. Zeeb
3d3ec17825 LinuxKPI: sk_buff: implement skb_queue_splice_tail_init()
Implement skb_queue_splice_tail_init() and SKB_DATA_ALIGN() as
needed by the mt76 wireless driver.

Sponsord by:	The FreeBD Foundation
MFC after:	3 days
Reviewed by:	emaste
Differential Revision: https://reviews.freebsd.org/D44590
2024-04-15 16:06:11 +00:00
Bjoern A. Zeeb
738c02ba24 LinuxKPI: add dev_alloc_pages()
Add dev_alloc_pages() though it seems a weird KPI, not passing a dev
despite its name.  Used by updated wireless driver.

Sponsored by:	The FreeBSD Foundation
MFC after:	3 days
Reviewed by:	emaste
Differential Revision: https://reviews.freebsd.org/D44588
2024-04-12 23:15:27 +00:00
Bjoern A. Zeeb
f674f016c0 LinuxKPI: add BITS_TO_BYTES()
Just like BITS_TO_LONG() ... used in rtw89.

Sponsored by:	The FreeBSD Foundation
MFC after:	3 days
Reviewed by:	emaste
Differential Revision: https://reviews.freebsd.org/D44603
2024-04-12 23:11:47 +00:00
Bjoern A. Zeeb
218b2ccf8c LinuxKPI: implement memzero_explicit()
Sponsored by:	The FreeBSD Foundation
MFC after:	3 days
Reviewed by:	emaste
Differential Revision: https://reviews.freebsd.org/D44586
2024-04-12 23:09:19 +00:00
Bjoern A. Zeeb
a1cdddcf15 LinuxKPI: add further constants to pci.h
Updated wireless drivers require more knowledge about PCI (BAR) bits.

Sponsored by:	The FreeBSD Foundation
MFC after:	3 days
Reviewed by:	jhb
Differential Revision: https://reviews.freebsd.org/D44573
2024-04-12 23:08:06 +00:00
Vladimir Kondratyev
4e7aa03b70 LinuxKPI: Stub sysfs_remove_link in linux/sysfs.h
sysfs_create_link is stubbed already. Stub sysfs_remove_link too to be
feature-complete.

Sponsored by:	Serenity Cyber Security, LLC
Reviewed by:	emaste
MFC after:	1 week
2024-04-08 09:47:43 +03:00
Vladimir Kondratyev
56041ee817 LinuxKPI: Add want_init_on_free to linux/mm.h
want_init_on_free returns if heap memory zeroing on free is enabled.
FreeBSD does not zeroes heap memory on free().

Sponsored by:	Serenity Cyber Security
Reviewed by:	emaste
MFC after:	1 week
2024-04-08 09:47:43 +03:00
Vladimir Kondratyev
f7ea333e2b LinuxKPI: Add ACPI_ID_LEN const to linux/mod_devicetable.h
Sponsored by:	Serenity Cyber Security, LLC
Reviewed by:	emaste
MFC after:	1 week
2024-04-08 09:47:43 +03:00
Vladimir Kondratyev
f1206503e5 LinuxKPI: Add pci_dev_id to linux/pci.h
It returns bus/device/function number for given PCI device.
Also add intermediate PCI_DEVID macro used in some drivers.

Sponsored by:	Serenity Cyber Security, LLC
Reviewed by:	emaste
MFC after:	1 week
2024-04-08 09:47:43 +03:00
Vladimir Kondratyev
e8f59f4d31 LinuxKPI: Add the accelerator PCIe class
Sponsored by:	Serenity Cyber Security, LLC
Reviewed by:	emaste
MFC after:	1 week
2024-04-08 09:47:43 +03:00
Vladimir Kondratyev
e0eeeca8b8 LinuxKPI: Add PTR_IF macro
Sponsored by:	Serenity Cyber Security, LLC
Reviewed by:	emaste
MFC after:	1 week
2024-04-08 09:47:42 +03:00
Vladimir Kondratyev
06902a4479 LinuxKPI: Add vm_flags_(clear|set) functions
Sponsored by:	Serenity Cyber Security, LLC
Reviewed by:	emaste
MFC after:	1 week
2024-04-08 09:47:42 +03:00
Vladimir Kondratyev
9289c1f6f1 LinuxKPI: Add get_random_u32_below function
get_random_u32_below returns a random integer in the interval [0, ceil),
with uniform distribution.

Sponsored by:	Serenity CyberSecurity, LLC
Reviewed by:	emaste
MFC after:	1 week
2024-04-08 09:47:42 +03:00
Vladimir Kondratyev
38c276a43f LinuxKPI: Add VM_ACCESS_FLAGS define to linux/mm.h
VM_ACCESS_FLAGS is a basic access permission flags.

Sponsored by:	Serenity CyberSecurity, LLC
Reviewed by:	emaste
MFC after:	1 week
2024-04-08 09:47:42 +03:00
Vladimir Kondratyev
3208d4ad2b LinuxKPI: Import vanilla linux/overflow.h
It is dual-licensed (GPLv2 & MIT) and self-contained header file.
No need to reimplement it.

Sponsored by:	Serenity CyberSecurity, LLC
Reviewed by:	emaste
MFC after:	1 week
2024-04-08 09:47:42 +03:00
Vladimir Kondratyev
8cfd1dd821 LinuxKPI: Move [SU](8|16|32|64)_(MAX|MIN) defines to linux/limits.h
Some source files get them from linux/limits.h directly rather than from
linux/kernel.h.
While here replace Linux constant values with sys/stdint.h provided ones.

Sponsored by:	Serenity Cyber Security, LLC
Reviewed by:	emaste
MFC after:	1 week
2024-04-08 09:47:42 +03:00
Vladimir Kondratyev
1970388766 LinuxKPI: Add strnchr function
strnchr() finds a character in a length limited string.

Sponsored by:	Serenity CyberSecurity, LLC
Reviewed by:	emaste
MFC after:	1 month
2024-04-08 09:47:42 +03:00
Vladimir Kondratyev
aafe4126f7 LinuxKPI: Add ms_to_ktime
Sponsored by:	Serenity CyberSecurity, LLC
Reviewed by:	emaste
MFC after:	1 week
2024-04-08 09:47:42 +03:00
Zhenlei Huang
317cc829ee LinuxKPI: Remove the temporary variable fileid from the macro request_module
The variable fileid stores the result from kern_kldload() but never gets
used. Since the third parameter `*fileid` of kern_kldload() can be NULL,
this unused variable can be safely removed.

No functional change intended.

Reviewed by:	emaste, bz, #linuxkpi
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D44583
2024-04-06 00:26:09 +08:00
Bjoern A. Zeeb
ce707e674f LinuxKPI: add PCI_VENDOR_ID_ITTIM
Add PCI_VENDOR_ID_ITTIM as needed by mt76.

Sponsored by:	The FreeBSD Foundation
MFC after:	3 days
2024-04-02 10:03:31 +00:00
Tom Coldrick
d3befb534b LinuxKPI: Queue skbuffs at tail in __skb_queue_tail
Correct skb_queue_tail to queue the buffer at the tail of the skbuff.
The skbuff is a circular doubly-linked list, and we call with a pointer
to the head of the list.  Thus queueing before the head gives us a
queueing at the tail.

As a motivating factor, the current behaviour (queueing at the head) was
causing frequent kernel panics from my RTL8822BE wireless card, which
uses the rtw88 driver.  Interrupts can cause buffers to be added to the
rtwdev c2h_queue while the queue is being drained in rtw_c2h_work.
Queueing at the head would leave the nascent entry in the linked list
pointing to the old, now freed, memory for the buffer being processed.
When rtw_c2h_work is next called, we try reading this and so panic.

Reviewed by:	emaste, bz
MFC after:	1 week
Differential Revision: https://reviews.freebsd.org/D44192
2024-03-04 09:33:12 -05:00
Bjoern A. Zeeb
7061745867 LinuxKPI: sort dev_<loglevel>() functions
Sort the dev_<loglevel> functions by loglevel order, add the dev_alert()
version and an indentation change.

No functional changes.

MFC after:	3 days
Reviewed by:	emaste
Differential Revision: https://reviews.freebsd.org/D43719
2024-02-14 22:05:21 +00:00
Bjoern A. Zeeb
53eb2c63c9 LinuxKPI: 802.11: correct HT protection fields
It seems during the initial buildup of the file, the defines were
either mixed or not flagged as "FIXME".
Define the values through to the net80211 definitions and also
annotate them by at least some standards reference.

MFC after:	3 days
Fixes:		6b4cac814e
Reviewed by:	cc
Differential Revision: https://reviews.freebsd.org/D43658
2024-02-14 19:36:30 +00:00
Vladimir Kondratyev
c0b8047bdc LinuxKPI: Allow kmalloc to be called when FPU protection is enabled
Amdgpu driver does a lot of memory allocations in FPU-protected sections
of code for certain display cores, e.g. for DCN30. This does not work
on FreeBSD as its malloc function can not be run within a critical
section. Check this condition and temporally exit from FPU-protected
context to workaround issue and reduce source code patching.

Sponsored by:	Serenity Cyber Security, LLC
Reviewed by:	manu (previous version)
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D42822
2024-02-11 01:01:50 +03:00
Vladimir Kondratyev
db65db64fb Revert "LinuxKPI: Add explicit software context to FPU sections"
kmalloc() support within FPU sections will be redone in other way.

This reverts commit 5a3bd28167.

Sponsored by:	Serenity Cyber Security, LLC
Requested by:	jhb
2024-02-11 01:01:50 +03:00
Vladimir Kondratyev
7f1a7506cb Revert "LinuxKPI: Do not use explicit context in FPU sections on powerpc64"
kmalloc() support within FPU sections will be redone in other way.

This reverts commit b4efc62776.

Sponsored by:	Serenity Cyber Security, LLC
Requested by:	jhb
2024-02-11 01:01:50 +03:00
Bjoern A. Zeeb
2d9d35837d LinuxKPI: implement memset_after()
memset_after() is needed by iwlwifi d3.c.

MFC after:	3 days
Reviewed by:	emaste
Differential Revision: https://reviews.freebsd.org/D43644
2024-02-03 13:50:32 +00:00
Bjoern A. Zeeb
2f029bf911 LinuxKPI: 802.11: extend ieee80211_key_seq and WLAN_KEY_LEN_* defines
For iwlwifi(4) d3.c we need another struct in struct ieee80211_key_seq
and related WLAN_KEY_LEN_* defines in.

MFC after:	3 days
Reviewed by:	cc, emaste
Differential Revision: https://reviews.freebsd.org/D43646
2024-02-03 13:31:50 +00:00
Gleb Smirnoff
f79a8585bb sockets: garbage collect SS_ISCONFIRMING
Fixes:	8df32b19de
2024-01-30 10:38:33 -08:00
Ed Maste
7e77089dcc linuxkpi: remove invalid KASSERT from hash_add_rcu
hash_add_rcu asserted that the node's prev pointer was NULL in an
attempt to detect addition of a node already on a list, but the caller
is not required to provide a zeroed node.

Reported in https://github.com/freebsd/drm-kmod/issues/282

Reviewed by:	bz, manu
Sponsored by:	The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D43645
2024-01-29 11:40:17 -05:00
Mark Johnston
344a04ad2c linuxkpi: Annotate an unused variable as such
This addresses a -Wunused-but-set-variable warning in the gcc builds.

No functional change intended.

Reported by:	Jenkins
2023-12-27 10:11:33 -05:00
Vladimir Kondratyev
dbca442414 LinuxKPI: Constify src parameter of bitmap_copy
in bitmap_from_arr32() to fix build on 32 bit archs.

Sponsored by:	Serenity Cyber Security, LLC
Fixes:	5ae2e6f913 ("LinuxKPI: Add bitmap_intersects(), bitmap_from_arr32()")
MFC after:	1 week
2023-12-24 15:48:06 +03:00
Vladimir Kondratyev
73ccd188c4 LinuxKPI: Define ioread64() in linux/io.h only on 64 bit archs.
32 bit archs includes nonatomic version from linux/io-64-nonatomic-*.h

Sponsored by:   Serenity Cyber Security, LLC
Fixes:		dcfc983373 ("LinuxKPI: Implement ioread64()")
MFC after:      1 week
2023-12-24 15:48:06 +03:00