--- NVIDIA-Linux-x86_64-340.108-patched-orig/kernel/nv-acpi.c	2023-05-07 06:04:38.423644992 -0400
+++ NVIDIA-Linux-x86_64-340.108/kernel/nv-acpi.c	2023-05-07 07:17:03.749971271 -0400
@@ -178,6 +178,51 @@
     return 0;
 }
 
+static struct nv_acpi_add_enumerated_data {
+ nv_acpi_t *object;
+ int *counter;
+};
+static int nv_acpi_add_enumerated(struct acpi_device *dev, void *data)
+{
+    struct nv_acpi_add_enumerated_data *rcvd_data = data;
+    nv_acpi_t *pNvAcpiObject = rcvd_data->object;
+    int *device_counter = rcvd_data->counter;
+    acpi_status status = -1;
+    nv_acpi_integer_t device_id = 0;
+    if (!dev)
+        return 0;
+    if (*device_counter == NV_MAXNUM_DISPLAY_DEVICES) {
+        nv_printf(NV_DBG_ERRORS, 
+            "NVRM: nv_acpi_add: Total number of devices cannot exceed %d\n", 
+            NV_MAXNUM_DISPLAY_DEVICES);
+        return 1;
+    }
+    status =
+        acpi_evaluate_integer(dev->handle, "_ADR", NULL, &device_id);
+    if (ACPI_FAILURE(status))
+        /* Couldnt query device_id for this device */
+        return 0;
+
+    device_id = (device_id & 0xffff);
+
+    if ((device_id != 0x100) && /* Not a known CRT device-id */ 
+        (device_id != 0x200) && /* Not a known TV device-id */ 
+        (device_id != 0x0110) && (device_id != 0x0118) && (device_id != 0x0400) && /* Not an LCD*/
+        (device_id != 0x0111) && (device_id != 0x0120) && (device_id != 0x0300)) /* Not a known DVI device-id */ 
+    {
+        /* This isnt a known device Id. 
+           Do default switching on this system. */
+        pNvAcpiObject->default_display_mask = 1;
+        return 1;
+    }
+
+    pNvAcpiObject->pNvVideo[*device_counter].dev_id = device_id;
+    pNvAcpiObject->pNvVideo[*device_counter].dev_handle = dev->handle;
+
+    (*device_counter)++;
+    return 0;
+}
+
 static int nv_acpi_add(struct acpi_device *device)
 {
     /*
@@ -190,8 +235,6 @@
     union acpi_object control_argument_0 = { ACPI_TYPE_INTEGER };
     struct acpi_object_list control_argument_list = { 0, NULL };
     nv_stack_t *sp = NULL;
-    struct list_head *node, *next;
-    nv_acpi_integer_t device_id = 0;
     int device_counter = 0;
 
     NV_KMEM_CACHE_ALLOC_STACK(sp);
@@ -220,47 +263,11 @@
 
     // grab handles to all the important nodes representing devices
 
-    list_for_each_safe(node, next, &device->children) 
-    {
-        struct acpi_device *dev =
-            list_entry(node, struct acpi_device, node);
-
-        if (!dev)
-            continue;
-
-        if (device_counter == NV_MAXNUM_DISPLAY_DEVICES)
-        {
-            nv_printf(NV_DBG_ERRORS, 
-                      "NVRM: nv_acpi_add: Total number of devices cannot exceed %d\n", 
-                      NV_MAXNUM_DISPLAY_DEVICES);
-            break;
-        }
-
-        status =
-            acpi_evaluate_integer(dev->handle, "_ADR", NULL, &device_id);
-        if (ACPI_FAILURE(status))
-            /* Couldnt query device_id for this device */
-            continue;
-
-        device_id = (device_id & 0xffff);
-
-        if ((device_id != 0x100) && /* Not a known CRT device-id */ 
-            (device_id != 0x200) && /* Not a known TV device-id */ 
-            (device_id != 0x0110) && (device_id != 0x0118) && (device_id != 0x0400) && /* Not an LCD*/
-            (device_id != 0x0111) && (device_id != 0x0120) && (device_id != 0x0300)) /* Not a known DVI device-id */ 
-        {
-            /* This isnt a known device Id. 
-               Do default switching on this system. */
-            pNvAcpiObject->default_display_mask = 1;
-            break;
-        }
-
-        pNvAcpiObject->pNvVideo[device_counter].dev_id = device_id;
-        pNvAcpiObject->pNvVideo[device_counter].dev_handle = dev->handle;
-    
-        device_counter++;
-
-    }
+    struct nv_acpi_add_enumerated_data data = {
+        .object = pNvAcpiObject,
+        .counter = &device_counter,
+    };
+    acpi_dev_for_each_child(device, nv_acpi_add_enumerated, &data);
 
     // arg 0, bits 1:0, 0 = enable events
     control_argument_0.integer.type = ACPI_TYPE_INTEGER;
@@ -1185,6 +1192,29 @@
     return status;
 }
 
+static int nv_acpi_ddc_method_enumerated(struct acpi_device *dev, void *data)
+{
+    acpi_handle *lcd_dev_handle = data;
+    acpi_status status;
+    nv_acpi_integer_t device_id = 0;
+    if (!dev)
+        return 0;
+    status = acpi_evaluate_integer(dev->handle, "_ADR", NULL, &device_id);
+    if (ACPI_FAILURE(status))
+        /* Couldnt query device_id for this device */
+        return 0;
+
+    device_id = (device_id & 0xffff);
+
+    if ((device_id == 0x0110) || (device_id == 0x0118) || (device_id == 0x0400)) /* Only for an LCD*/
+    {
+        *lcd_dev_handle = dev->handle;
+        nv_printf(NV_DBG_INFO, "NVRM: %s Found LCD: %x\n", __FUNCTION__, device_id);
+        return 1;
+    }
+    return 0;
+}
+
 /*
  * This function executes a _DDC ACPI method.
  */
@@ -1200,8 +1230,6 @@
     union acpi_object *ddc;
     union acpi_object ddc_arg0 = { ACPI_TYPE_INTEGER };
     struct acpi_object_list input = { 1, &ddc_arg0 };
-    struct list_head *node, *next;
-    nv_acpi_integer_t device_id = 0;
     NvU32 i;
     acpi_handle dev_handle  = NULL;
     acpi_handle lcd_dev_handle  = NULL;
@@ -1227,29 +1255,7 @@
         return RM_ERR_NOT_SUPPORTED;
     }
 
-    list_for_each_safe(node, next, &device->children) 
-    {
-        struct acpi_device *dev =
-            list_entry(node, struct acpi_device, node);
-
-        if (!dev)
-            continue;
-
-        status = acpi_evaluate_integer(dev->handle, "_ADR", NULL, &device_id);
-        if (ACPI_FAILURE(status))
-            /* Couldnt query device_id for this device */
-            continue;
-
-        device_id = (device_id & 0xffff);
-
-        if ((device_id == 0x0110) || (device_id == 0x0118) || (device_id == 0x0400)) /* Only for an LCD*/
-        {
-            lcd_dev_handle = dev->handle;
-            nv_printf(NV_DBG_INFO, "NVRM: %s Found LCD: %x\n", __FUNCTION__, device_id);
-            break;
-        }
-
-    }
+    acpi_dev_for_each_child(device, nv_acpi_ddc_method_enumerated, &lcd_dev_handle);
 
     if (lcd_dev_handle == NULL)
     {
